|
闲着没有事情,写的一个tictactoe的小游戏。windows下dev-cpp编译通过的。
[php]
tictactoe.h
/*******************************TicTacToe V1.0**************************/
/***********************************************************************/
#ifndef __TICTACTOE__H
#define __TICTACTOE__H
#include <string>
#include <iostream>
#include <exception>
namespace com_ybokham_tictactoe
{
using namespace std;
struct player
{
char useCh; //Represent the player's flag,such as 'X' or 'O'
string name;
player(player &someone) //copy constructor
{
useCh = someone.useCh;
name = someone.name;
}
player(char ch = '\0',string nm = "") //default constructor
{
useCh = ch;
string name = nm;
}
};
class TicTacToe
{
private:
int sum; // sum = 9
char matrix[3][3]; //TicTacToe matrix
bool isOver; //flag to determin if the game is over
player player1,player2;
public:
void gameBegin(); //Begin the game
void showScreen(); //Show the game interface
bool ifWin(player someone); //check if win, the return type can be void
TicTacToe(); //constructor
void getPlayerAct(player someome) throw(exception); //get player's action
};
void TicTacToe::gameBegin()
{
cout << "Game begins now!!!" << endl;
int i = 1;
player temp;
while(!isOver)
{
showScreen();
if((i%2)==1)
{
temp = player1;
//temp.useCh = player1.useCh;
//temp.name = player1.name;
}
else
{
temp = player2;
//temp.useCh = player2.useCh;
//temp.name = player2.name;
}
getPlayerAct(temp);
i++;
ifWin(temp);
}
}
TicTacToe::TicTacToe()
{
for (int m = 0;m<3;m++)
for (int n=0;n<3;n++)
matrix[m][n]= '\0'; //Initialize the matrix[][]
sum = 0;
isOver = false;
player1.useCh = 'X';
player1.name = "player1";
player2.useCh = 'O';
player2.name = "player2";
}
void TicTacToe::showScreen() //show the player's screen
{
cout<<"\n\t\t 1 2 3\n"<<endl;
cout<<"\t\t 1 "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;
cout<<"\t\t ---|---|---\n";
cout<<"\t\t 2 "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;
cout<<"\t\t ---|---|---\n";
cout<<"\t\t 3 "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";
}
bool TicTacToe::ifWin(player someone)
{
bool flag = false;
if (matrix[0][0]==someone.useCh && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
cout<<someone.name<<" wins!!!"<<endl;
flag = true;
}
if (matrix[2][0]==someone.useCh && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (matrix[0][0]==someone.useCh && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (matrix[0][1]==someone.useCh && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (matrix[0][2]==someone.useCh && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (matrix[0][0]==someone.useCh && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (matrix[1][0]==someone.useCh && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (matrix[2][0]==someone.useCh && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<someone.name<<" wins!!!"<<endl;flag = true;}
if (sum == 9){cout<<"The game is over and no one wins, hahaha, you both stink!!!"<<endl;flag = true; } //sum=9 because there are only 9 boxes in the game
isOver = flag;
return flag;
}
void TicTacToe::getPlayerAct(player someone) throw(exception)
{
int Row, Column;
cout << "It's " + someone.name + "'s turn!!!" << endl;
cout << "Your flag is " ;
cout << someone.useCh << endl;
do
{
cout << "Choose the row and column : " << endl;
cout << "Row: " << endl;
cin >> Row ;
if(Row<0 || Row>9) throw exception() ;
cout << "Column: " << endl;
cin >> Column;
if(Row<0 || Row>9) throw exception() ;
if (Row>3||Column>3||(matrix[Row-1][Column-1] != '\0'))
cout << "You locate wrong place,choose it again!!!" << endl;
}while((matrix[Row-1][Column-1] != '\0')||Row>3||Column>3);
matrix[Row-1][Column-1] = someone.useCh ;
}
}
#endif
main.cpp
#include "tictactoe.h"
using namespace com_ybokham_tictactoe;
int main(int argc, char *argv[])
{
try
{
TicTacToe game;
game.gameBegin();
}
catch(exception &e)
{
cout << "STL Exception " << e.what() << endl;
return 1;
}
catch(...)
{
cout << "other exception" << endl;
return 2;
}
return 0;
}
[/php] |
|