Created
June 30, 2014 02:12
-
-
Save computerquip/e387a67e85cc559c6135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include <stdio.h> | |
// Prototype the functions | |
int CheckEndOfGame(int * squares); | |
int ChooseSquare(int * squares); | |
int MakeMoves(int * ctrMoves, int * playr, int * squares); | |
int main() | |
{ /* High Level - play one or more games: | |
Players make moves until one wins or it's a draw. | |
Announce the winner (or draw). | |
Ask player to play again; play game or stop. | |
*/ | |
// Declare variables | |
int gameOver = 0; // no, game is not over yet | |
int playGame = 1; // yes, play the game | |
do // Play Game(s) | |
{ // Store all choices in an array of 9 squares (squares[9]) | |
// initialize the array with spaces (space is ASCII 32) | |
int squares[9] = {32, 32, 32, 32, 32, 32, 32, 32, 32}; | |
// ("X" is ASCII 88) | |
// ("O" is ASCII 79) | |
int ctrMoves = 0; // no moves yet | |
int player = 0; // [[see "if" in MakeMoves]] | |
do // Make one move | |
{ gameOver = MakeMoves(&ctrMoves, &player, squares); | |
} | |
while (!gameOver); | |
// draw the board (before declaring a winner), then display | |
printf("\n"); | |
printf(" %c | %c | %c \n", squares[6], squares[7], squares[8]); | |
printf("-----------\n"); | |
printf(" %c | %c | %c \n", squares[3], squares[4], squares[5]); | |
printf("-----------\n"); | |
printf(" %c | %c | %c \n", squares[0], squares[1], squares[2]); | |
printf("\n"); | |
if (gameOver == 1) | |
printf("The game ended in a draw.\n"); | |
else if (gameOver == 2) | |
printf("Congratulations! You won!\n"); | |
else | |
printf("The computer won.\n"); | |
printf("Play again? (1 is Yes, 0 is No). "); | |
scanf("%d", &playGame); | |
} while (playGame == 1); | |
/* | |
Wrapup | |
"Thanks" | |
clean up | |
Other | |
*/ | |
return 0; | |
} | |
int MakeMoves(int * ctrMoves, int * playr, int * squares) | |
{ /* Middle Level - make one move, check for winner: | |
When X takes a turn, draw the board, ask for chosen square, store it. | |
When O takes a turn, program chooses square, store it if game not over. | |
*/ | |
int endTheGame = 0; // no, game has not ended yet | |
int thisSquare = 0; | |
int drawTheBoard = 0; | |
// get a choice (from person or program), then store it in the array | |
if (*playr == 0) | |
{ printf("\n"); | |
printf(" %c | %c | %c \n", squares[6], squares[7], squares[8]); | |
printf("-----------\n"); | |
printf(" %c | %c | %c \n", squares[3], squares[4], squares[5]); | |
printf("-----------\n"); | |
printf(" %c | %c | %c \n", squares[0], squares[1], squares[2]); | |
printf("\n"); | |
*playr = 1; // it's now the person's turn | |
printf("Choose a square (1 - 9). \n"); | |
scanf("%d", &thisSquare); | |
squares[thisSquare - 1] = 88; | |
// subtract 1 from thisSquare to get the array subscript | |
// eg, center square, 5, is square[4] | |
} | |
else | |
{ *playr = 0; // it's now the program's turn | |
thisSquare = ChooseSquare(squares); | |
if (thisSquare == 0) | |
*ctrMoves = 9; // tie - no squares left; game over | |
else | |
squares[thisSquare - 1] = 79; | |
// subtract 1 from thisSquare to get the array subscript | |
// eg, center square, 5, is square[4] | |
} | |
*ctrMoves = *ctrMoves + 1; | |
if (*ctrMoves > 9) | |
endTheGame = 1; // tie - no squares left; game over | |
else if (*ctrMoves >= 5) | |
endTheGame = CheckForWinner(squares); | |
return endTheGame; | |
} | |
int ChooseSquare(int * squares) | |
{ /* Low Level (1) - find the best empty square | |
(eg, squares[4] == 32 means center square is open) | |
*/ | |
if (squares[4] == 32) | |
return 5; | |
else | |
if (squares[0] == 32) | |
return 1; | |
else | |
if (squares[2] == 32) | |
return 3; | |
else | |
if (squares[6] == 32) | |
return 7; | |
else | |
if (squares[8] == 32) | |
return 9; | |
else | |
if (squares[1] == 32) | |
return 2; | |
else | |
if (squares[3] == 32) | |
return 4; | |
else | |
if (squares[5] == 32) | |
return 6; | |
else | |
if (squares[7] == 32) | |
return 8; | |
else | |
return 0; // tie - no squares left; game over | |
} | |
int CheckForWinner(int * squares) | |
{ /* Low Level (2) - check whether person won, program won, or draw | |
winners: 1-2-3 | 4-5-6 | 7-8-9 || 1-4-7 | 2-5-8 | 3-6-9 || 1-5-9 | 3-5-7 | |
0 means game not over; 1 means draw; 2 means person won; 3 means program won | |
*/ | |
if (squares[0] == squares[1] && squares[1] == squares[2] && squares[2] != 32) | |
{ // top row | |
if (squares[2] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[3] == squares[4] && squares[4] == squares[5] && squares[5] != 32) | |
{ // middle row | |
if (squares[5] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[6] == squares[7] && squares[7] == squares[8] && squares[8] != 32) | |
{ // bottom row | |
if (squares[8] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[0] == squares[3] && squares[3] == squares[6] && squares[6] != 32) | |
{ // left column | |
if (squares[6] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[1] == squares[4] && squares[4] == squares[7] && squares[7] != 32) | |
{ // middle column | |
if (squares[7] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[2] == squares[5] && squares[5] == squares[8] && squares[8] != 32) | |
{ // right column | |
if (squares[8] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[0] == squares[4] && squares[4] == squares[8] && squares[8] != 32) | |
{ // top left to bottom right | |
if (squares[8] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else if (squares[2] == squares[4] && squares[4] == squares[6] && squares[6] != 32) | |
{ // top right to bottom left | |
if (squares[6] == 88) // X won | |
return 2; | |
else | |
return 3; | |
} | |
else | |
return 0; // game not over | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment