1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
enum field{
X = -1 , O , field_1=1, field_2=2, field_3 = 3, field_4 = 4, field_5 = 5, field_6 = 6, field_7 = 7, field_8 = 8, field_9 = 9,field_10=10,field_11=11, field_12=12,field_13=13,field_14=14,field_15=15,field_16=16,field_17=17,
field_18=18,field_19=19,field_20=20,field_21=21,field_22=22,field_23=23,
field_24=24,field_25=25
};
int row , column ;
int check_result(field board[][5])
{same codes as previous}
void display_board_update(field board[][5],int row ,int column)
{same as before}
void play_move(field board[][5])
{
int player = 1 , p ;
int choose_move ;
field mark_field ;
do
{display_board_update(board,row,column) ;
player = (player % 2) ? 1 : 2 ;
cout << "\n\n\n\nplayer " << player << " , please choose your move : " ;
cin >> choose_move ;
mark_field = (player % 2) ? X : O ;
player++ ;
while (choose_move != board[0][0] &&choose_move != board[0][1] &&choose_move != board[0][2] &&choose_move != board[0][3] &&choose_move != board[0][4] && choose_move != board[1][0] &&choose_move != board[1][1] &&choose_move != board[1][2] &&choose_move != board[1][3] &&choose_move != board[1][4] &&choose_move != board[2][0] &&choose_move != board[2][1] &&choose_move != board[2][2] &&choose_move != board[2][3] &&choose_move != board[2][4] &&choose_move != board[3][0] &&choose_move != board[3][1] &&choose_move != board[3][2] &&choose_move != board[3][3] &&choose_move != board[3][4] &&choose_move != board[4][0] &&choose_move != board[4][1] &&choose_move != board[4][2] &&choose_move != board[4][3] &&choose_move != board[4][4])
{ player-- ;
cout << "Invalid Move.Please Press 'enter key' and choose again : " << endl;
cout << "player " << player << " choose your move :" ;
cin >> choose_move ;
player++ ;}
for (int i = 0 ; i < row ;i++)
{for (int j = 0 ; j < column ; j++)
{if ( choose_move == board[i][j])
{board[i][j] = mark_field ;}
}
}
p = check_result(board);
if (p == 0)
{display_board_update(board,row,column) ;
cout << "\n\n\n\nResult : The Game Is Draw. \n" ;
break ;}
}while ( p != 1) ;
if (p == 1)
{ display_board_update(board,row,column) ;
cout << "\n\n\n\nResult : player " << --player << " is winner. \n" ;}
}
int main()
{
field board[5][5] ;
int x = 1 ;
cout << "Enter the size of board (e.g. 4 for 4 * 4 or 5 for 5*5), max size is 5 * 5 : " ;
cin >> row ;
while (row <3 || row >5)
{
cout << " \n\nyour entered board size is either less than the default size of 3 * 3 or more than the size of 5 * 5. \n please enter again. :" ;
cin >> row ;
}
column = row ;
for (int i = 0 ; i < row ; i++)
{ for (int j = 0 ; j < column ; j++)
{ switch (x) {
case 1 : board[i][j] = field_1 ; x++ ; break ;
case 2 : board[i][j] = field_2 ; x++ ; break ;
case 3 : board[i][j] = field_3 ; x++ ; break ;
case 4 : board[i][j] = field_4 ; x++ ; break ;
case 5 : board[i][j] = field_5 ; x++ ; break ;
case 6 : board[i][j] = field_6 ; x++ ; break ;
case 7 : board[i][j] = field_7 ; x++ ; break ;
case 8 : board[i][j] = field_8 ; x++ ; break ;
case 9 : board[i][j] = field_9 ; x++ ; break ;
case 10: board[i][j] =field_10 ; x++ ; break ;
case 11: board[i][j] =field_11 ; x++ ; break ;
case 12: board[i][j] =field_12 ; x++ ; break ;
case 13: board[i][j] =field_13 ; x++ ; break ;
case 14: board[i][j] =field_14 ; x++ ; break ;
case 15: board[i][j] =field_15 ; x++ ; break ;
case 16: board[i][j] =field_16 ; x++ ; break ;
case 17: board[i][j] =field_17 ; x++ ; break ;
case 18: board[i][j] =field_18 ; x++ ; break ;
case 19: board[i][j] =field_19 ; x++ ; break ;
case 20: board[i][j] =field_20 ; x++ ; break ;
case 21: board[i][j] =field_21 ; x++ ; break ;
case 22: board[i][j] =field_22 ; x++ ; break ;
case 23: board[i][j] =field_23 ; x++ ; break ;
case 24: board[i][j] =field_24 ; x++ ; break ;
case 25: board[i][j] =field_25 ; x++ ; break ;}
}
}
play_move(board);
}
|