Int main (void)



Download 13.29 Kb.
Date11.12.2021
Size13.29 Kb.
#57891
Provvv
Provvv

/** TicTacToe game.Create a class TicTacToe that will enable you to write a complete program to play the game of tic-tac-toe.The class contains as private data a 3-by-3

two-dimensional array of integers.The constructor should initialize the empty board to all zeros.Allow two human players.Wherever the first player moves,place a 1

in the specified square.Place a 2 wherever the second player moves.Each move must be to an empty square.After each move,determine if the game has been won or is draw.

If you feel ambitious,modify your program so that the computer makes the moves for one of the players.Also,allow the player to specify whether he or she wants to go first

or second.If you feel exxceptionally mbitious,develop a program that will play three dimensional tic-tac-toe on a 4-by-4 board.*/

#include

int main (void)

{

int player = 0;



int winner = 0;

int choice = 0;

int row = 0;

int column = 0;

int line = 0;

char board [3][3] = {

{'1','2','3'},

{'4','5','6'},

{'7','8','9'}

};

for (int i = 0; i<9 && winner==0; i++)



{

printf("\n\n");

printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);

printf("---|---|---\n");

printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);

printf("---|---|---\n");

printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

player = i%2 + 1;

do

{

printf("\nPlayer %d, please enter the number of the square "



"where you want to place your %c: ",

player,(player==1)?'X':'O');

scanf("%d", &choice);

row = --choice/3;

column = choice%3;

}while(choice<0 || choice>9 || board [row][column]>'9');

board[row][column] = (player == 1) ? 'X' : 'O';

if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||

(board[0][2]==board[1][1] && board[0][2]==board[2][0]))

winner = player;

else

for(line = 0; line <=2; line++)



if((board[line][0]==board[line][1] && board[line][0]==board[line][2])||

(board[0][line]==board[1][line] && board[0][line]==board[2][line]))

winner = player;

}

printf("\n\n");



printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);

printf("---|---|---\n");

printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);

printf("---|---|---\n");

printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

if(winner==0)

printf("The game is a draw\n");

else


printf("Player %d has won\n", winner);

return 0;



}
Download 13.29 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page