iT邦幫忙

第 11 屆 iT 邦幫忙鐵人賽

DAY 10
0
自我挑戰組

資訊工程大補帖系列 第 10

資工補帖-Day 10-計算機概論 (C++)

正文

大 1 的上下學期程式語言分別是 C , C++
來 review 自己大一的 C++ 吧 XD

期末考考貪吃蛇的程式碼,剛好有找到自己練習下來的 code
基本上 C語言 跟 C++ 後面差很多,
C++ 會跟 JAVA 的工程師吵架 XDDD
講到物件導向就覺得要學的東西好多阿~~~~~

#include<iostream>

using namespace std;
int main(int argc, char** argv) {
	int in,sum; //設定輸入值、計算代數 
	while(true) //進入while的迴圈讓他可重複計算
	{
		//設定變數以及輸入值 
		cout<<"輸入一正整數:"; 
		cin>>in;
		cout<<in<<" = ";
		for(int x=2;x<=in;x++) //一個for迴圈尋找每個質數
		{
			while(in%x==0) //當for迴圈跑到該數值與輸入值整除時進入迴圈
			{
				if(in%(x*x)==0)//如果輸入值與該數值之平方整除時
				{
					sum=0;
					do{
						sum++;
						in/=x;
					}while(in%x==0);
					cout<<x<<" ^ "<<sum;
				}
				else
				{
					cout<<x;
					in/=x;
				} 
				if(in>1)
				{
					cout<<" * ";
				}
			
			} 
		}
		cout<<"\n";
	} 
	return 0;
} 

期末考是貪吃蛇!

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

class Point{
	private:
		int x;
		int y;
	
	public:
		Point(int a,int b)
		{
			x=a;
			y=b;	
		}
		
		Point()
		{
			x=0;
			y=0;
		}
	
		void setPoint(int a,int b)
		{
			x=a;
			y=b;
		}
	
		int getX()
		{
			return x;
		}
		
		int getY()
		{
			return y;
		}
};


class SnakeGame{
	private:
		char area[10][10]; //範圍 
		Point head;//頭 
		Point body[64];//身體 
		Point tail;//尾巴 
		Point food;//食物 
		int Length;//長度 
		
	public:
		SnakeGame()
		{
			Length=5;
			head.setPoint(4,3);
			body[0].setPoint(4,4);
			body[1].setPoint(4,5);
			body[2].setPoint(4,6);
			body[3].setPoint(4,7);
			tail.setPoint(4,7);
			food.setPoint(4,1);
			for(int i=0;i<10;i++)
			{
				for(int j=0;j<10;j++){
					if(i==0||i==9||j==0||j==9)
						area[i][j]='#';
					else
						area[i][j]=' ';
				}
			}
			
			area[head.getX()][head.getY()]='O';
			area[body[0].getX()][body[0].getY()]='O';
			area[body[1].getX()][body[1].getY()]='O';
			area[body[2].getX()][body[2].getY()]='O';
			area[tail.getX()][tail.getY()]='O';
			area[food.getX()][food.getY()]='*';

		}

		bool checkSpace(int step){
			if(step==76){  //左邊
				return !( area[head.getX() ][head.getY()-1]=='#'||area[head.getX()][head.getY()-1]=='O'); 
				}
			else if(step==85){//上面 
				return !(area[head.getX()-1][head.getY()]=='#'||area[head.getX()-1][head.getY()]=='O');
			} 
			else if(step==82){//右邊 
				return !(area[head.getX()][head.getY()+1]=='#'||area[head.getX()][head.getY()+1]=='O');
			} 	
			else if(step==68){//下面 
				return !(area[head.getX()+1][head.getY()]=='#'||area[head.getX()+1][head.getY()]=='O');
			} 	
		}
	
		void run(int step){
			tail.setPoint(body[Length-2].getX(),body[Length-2].getY());
			int tempX=tail.getX();
			int tempY=tail.getY();
			area[tail.getX()][tail.getY()]=' ';
			
			for(int i =Length-1;i>0;i--)
				{body[i].setPoint(body[i-1].getX(),body[i-1].getY());}
			
			body[0].setPoint(head.getX(),head.getY());
			
			if(step==76){  //左邊
				head.setPoint(head.getX(),head.getY()-1);}
			else if(step==85){//上面 
				head.setPoint(head.getX()-1,head.getY());}
			else if(step==82){//右邊 
				head.setPoint(head.getX(),head.getY()+1);}
			else if(step==68){//下面 
				head.setPoint(head.getX()+1,head.getY());}
				
			for(int i =0;i<Length-2;i++)
				{area[body[i].getX()][body[i].getY()]='O';}
			
			area[head.getX()][head.getY()]='O';
			
			Eatfood(tempX,tempY);
			}
		
		void printArea(){
			for(int i=0;i<10;i++){
				for(int j=0;j<10;j++)
					{cout<<area[i][j];}
				cout<<endl;
			}
		}
		
		void Foodset()
		{
			int foodX,foodY;
			srand((unsigned)time(NULL));
	
			foodX=(rand()%8)+1;
			foodY=(rand()%8)+1;
			
			for(int i =0;i<Length-2;i++)
			{
				if(foodX != body[i].getX() && foodY != body[i].getY())
					{if((foodX != head.getX() && foodY != head.getY()) ||( foodX!=0 && foodY!=0) ||	(foodX != tail.getX() && foodY != tail.getY()))
						{food.setPoint(foodX,foodY);}}
			}

					
			area[food.getX()][food.getY()]='*';
		}
		
		void Eatfood(int tempX,int tempY){
			if(head.getX()==food.getX()&&head.getY()==food.getY())
			{
				Length++;
				body[Length-1].setPoint(tempX,tempY);
				area[body[Length-1].getX()][body[Length-1].getY()]='O';
				Foodset();
			} 
		}	
		
		

};

int main(int argc, char** argv) {
	SnakeGame sg;
	char step;
	
	
	while(1)
	{
		sg.printArea();
		cout<<"請輸入移動方向(U/D/L/R):";
		cin>>step;
		
		if(step==76||step==85||step==82||step==68)
		{
			if(sg.checkSpace(step))
				{sg.run(step);
				}
		 	else{
		 	cout<<"GameOver"<<endl;
		 	break;}
		 } 
		system("cls");
	}
	return 0;
}

上一篇
資工補帖-Day 9-計算機概論(C語言)
下一篇
資工補帖-Day 11-密碼學之 VigenereCipher , LFSR
系列文
資訊工程大補帖30

尚未有邦友留言

立即登入留言