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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <stdio.h>
#include <stdlib.h>
char board[3][3]; // the tic tac toe board. Array 3 x 3
char check(void); // Function check
void init_board(void); //function inisalizing board
void get_player_move(void); //Function to get players move
void get_player2_move(void); //function to get player 2 move
void disp_board(void); //function to display board.
int main(void)
{
char done;
printf("Tic Tac Toe Game.\n");
printf("The World's Number 1 Online Game.\n\n");
char player1[10];
char player2[10];
printf("Player 1 Name : ", player1);
scanf("%s", &player1);
printf("Player 2 Name : ", player2);
scanf("%s", &player2);
done = ' ';
init_board();
do
{
disp_board();
get_player_move();
disp_board(); //Displays board after each move.
done = check(); // see if there is a winner.
if(done!= ' ') break; // winner
get_player2_move();
done = check(); // see if there is a winner after player 2 completes its move.
}
while(done== ' ');
if(done=='X') printf("%s has won!!!\n ", player1);
else printf("%s has won!!!!\n", player2);
system("pause");
}
// Initialize the board using an Array.
void init_board(void)
{
int i=0;
int j=0;
for(i=0; i<3; i++)
for(j=0; j<3; j++) board[i][j] = ' ';
}
// Get player 1 move
void get_player_move(void)
{
int x=0;
int y=0;
printf("%s, Enter Row (1-3): ", "player1");
scanf("%i", &x);
printf("%s Now Enter Column (1-3): ", "player1");
scanf("%i", &y);
x--; y--;
if(board[x][y]!= ' '){
printf("Invalid move, try again.\n");
get_player_move(); //If invalid move entered, it will go back to beginning of function(get_player_move)
}
else board[x][y] = 'X'; //if move is valid X will be placed onto board.
}
// Get player 2 move
void get_player2_move(void)
{
//x and y set up, x will be row, y will be column.
int x=0;
int y=0;
//Player 2 to enter thier coordinates.
printf("Player2, Enter Row (1-3): ");
scanf("%i", &x);
printf("Player2, Now Enter Column (1-3): ");
scanf("%i", &y);
x--; y--;
if(board[x][y]!= ' ')
{
printf("Invalid move, try again.\n");
get_player2_move();
}
else board[x][y] = 'O';
}
// Display the board on the screen
void disp_board(void)
{
int t=0;
for(t=0; t<3; t++)
{
printf(" %c | %c | %c ",board[t][0],
board[t][1], board [t][2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}
// See if there is a winner
char check(void)
{
int i=0;
for(i=0; i<3; i++) // checking the rows
if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
return board[i][0];
for(i=0; i<3; i++) // checking the columns
if(board[0][i]==board[1][i] && board[0][i]==board[2][i])
return board[0][i];
// This will check diagonals
if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
return board[0][0];
if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
return board[0][2];
return '0';
}
|