[ create a new paste ] login | about

Link: http://codepad.org/2VItJ1Bt    [ raw code | fork ]

C++, pasted on Jun 29:
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

class Game{
private:
	int me[3][3];
	bool playerproc(int x, int y, int player);
	bool comproc(int x, int y, int player);
	void draw();
public:
	void setsu();
	void init();
	void get_pos(char *player, int *x, int *y);
	void get_com(char *player, int *x, int *y);
	bool player1(int x, int y);
	bool player2(int x, int y);
	bool playcom(int x, int y);
	bool check_drawn();
	bool check();
};

void Game::setsu()
{
	cout << "○×ゲームです\n";
	cout << " 1 2 3\n";

	for(int i=0; i<3; i++)
	{
		cout << i + 1;
		cout <<"\n";
	}

}
//初期化
void Game::init()
{
	srand((unsigned int)time(NULL));

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			me[i][j] = 0;
		}
	}
}

//座標入力
void Game::get_pos(char *player, int *x, int *y)
{
	cout << player << "の横座標を入力してください:";
	cin >> *x;
	while(cin.fail())
	{
		cin.clear();
		cin.ignore(1024, '\n');
		cout << "文字が入ったのでやり直し\n";
		cout << player << "の横座標を入力してください:";
		cin >> *x;
	}

	cout << player << "の縦座標を入力してください:";
	cin >> *y;
	while(cin.fail())
	{
		cin.clear();
		cin.ignore(1024, '\n');
		cout << "文字が入ったのでやり直しです\n";
		cout << player << "の縦座標を入力してください:";
		cin >> *y;
	}
}

void Game::get_com(char *player, int *x, int *y)
{
	*x = rand() % 3;
	*y = rand() % 3;
}
//プレイヤ1
bool Game::player1(int x, int y)
{
	return playerproc(x, y, 1);
}

//プレイヤ2
bool Game::player2(int x, int y)
{
	return playerproc(x, y, 2);
}

//コンピュータ
bool Game::playcom(int x, int y)
{
	return comproc(x, y, 2);
}

//プレイヤの入力した座標をチェック
bool Game::playerproc(int x, int y, int player)
{
	if(x>=3 || y>=3 || x<0 || y<0)
	{
		cout << "超えてるよ\n";
		return false;
	}

	if(me[x][y] != 0)
	{
		cout << "使ってるよ\n";
		return false;
	}

	me[x][y] = player;
	draw();
	return true;
}

//コンピュータが入力した座標
bool Game::comproc(int x, int y, int player)
{
	do
	{

		if(me[x][y] != 0)
		{
			x = rand() % 3;
			y = rand() % 3;
		}
		
		if(x>=3 || y>=3 || x<0 || y<0)
		{
			x = rand() % 3;
			y = rand() % 3;
		}


	}while(me[x][y] != 0 || x>=3 || y>=3 || x<0 || y<0);

	me[x][y] = player;
	draw();
	return true;

}

//表と○と×を描画
void Game::draw()
{
	cout << " 1 2 3\n";

	for(int i=0; i<3; i++)
	{
		cout << i + 1;
		for(int j=0; j<3; j++)
		{
			if(me[i][j] == 1)
			{
				cout << "o";
			}
			else if(me[i][j] == 2)
			{
				cout << "x";
			}
			else
				cout << " ";
			cout << " ";
		}
		cout << "\n";
	}
}


//引分けかチェック
bool Game::check_drawn()
{
	bool drawn = true;

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			if(me[i][j] == 0)
			{
				drawn = false;
				break;
			}
		}
	}

	return drawn;
}

//勝敗のチェック
bool Game::check()
{
	for(int i=0; i<3; i++)
	{
		if((me[i][0] != 0) && (me[i][0] == me[i][1] && me[i][0] == me[i][2])) return true;
	}

	for(int j=0 ; j<3 ; j++)
	{
		if((me[0][j] != 0) && (me[0][j] == me[1][j] && me[0][j] == me[2][j])) return true;
	}

	if((me[0][0] != 0) && (me[0][0] == me[1][1]) && (me[0][0] == me[2][2])) return true;
	if((me[2][0] != 0) && (me[2][0] == me[1][1]) && (me[2][0] == me[0][2])) return true;

	return false;
}

int main()
{
	int x, y;
	int p1win=0, p2win=0, draw=0;
	int con = 1;
	int battle;

	Game g;

	g.setsu();

	do
	{
		cout << "1Pと2Pとの対戦にしますか?\n";
		cout << "1Pとコンピュータとの対戦にしますか?\n";
		cout << "プレイヤ同士の対戦なら1を、コンピュータとの対戦なら2を入力してください:";
		cin >> battle;

		g.init();

		//1Pと2Pとの対戦
		if(battle == 1)
		{
			while(1)
			{
				do
				{
					g.get_pos("○", &x, &y);
				}while(g.player1(x-1, y-1) == false);

				if(g.check())
				{
					cout << "プレイヤ1の勝ち\n";
					p1win++;
					break;
				}

				if(g.check_drawn())
				{
					cout << "引分け\n";
					draw++;
					break;
				}

				do
				{
					g.get_pos("×", &x, &y);
				}while(g.player2(x-1, y-1) == false);

				if(g.check())
				{
					cout << "プレイヤ2の勝ち\n";
					p2win++;
					break;
				}

				if(g.check_drawn())
				{
					cout << "引分け\n";
					draw++;
					break;
				}
				
				}
		}

		//1Pとコンピュータtの対戦
		if(battle == 2)
		{
			while(1)
			{
				do
				{
					g.get_pos("○", &x, &y);
				}while(g.player1(x-1, y-1) == false);

				if(g.check())
				{
					cout << "プレイヤ1の勝ち\n";
					p1win++;
					break;
				}

				if(g.check_drawn())
				{
					cout << "引分け\n";
					draw++;
					break;
				}

				do
				{
					g.get_com("×", &x, &y);
				}while(g.playcom(x-1, y-1) == false);

				if(g.check())
				{
					cout << "プレイヤ2の勝ち\n";
					p2win++;
					break;
				}

				if(g.check_drawn())
				{
					cout << "引分け\n";
					draw++;
					break;
				}
				
				}
			}
		
		

		cout << "1Pの勝ち数:" << p1win << "\n";
		cout << "2Pの勝ち数:" << p2win << "\n";
		cout << "ドロー:" << draw << "\n";

		cout << "続けないのなら0を押してね:";
		cin >> con;

	}while(con != 0);

	return 0;
}


Create a new paste based on this one


Comments: