To achieve the three-game
First, the realization of ideas
1, the array initialization
It is the squares of the three-game format, the received data two-dimensional array. With 'O' represents the child in the computer, 'X' on behalf of the sub-player. Not erupted initialization '' (space). The two-dimensional array of "char" type, the size of char board [3] [3].
2, the print board
print board Tic-Tac-out, and in order to display the data in the middle of each cell, separated by spaces ( "% c |") formatting checkerboard "|" for forming the vertical, and then print "- - - | "used to form the line. The for loop with two logically linked, to print out "well." While "% c" is initialized at '' (space)
3, the lower sub-player
<1> is represented by the sub-players in the array coordinates, a coordinate input prompt enter :( :), the format of the input (11), in the range of 1 to 3.
<2> players erupted when, if they had been to coordinate the child's return with an error, re-enter.
<3> If the coordinate input exceeds the range, an error, re-enter.
<4> print board, the coordinates of the lower sub player replaced with 'X'.
4, the lower sub computer
<1> Computer Now, using the range of 1 to 3, under conditions of randomly generated coordinate, coordinate of the child if they have been to, re generated coordinates conditions, where the use of debug cycle.
<2> has a lower sub-computer priority rule:
A, is a randomly generated first erupted computer, while the computer is in the upper hand, and the second is randomly generated.
B, determines whether there are two 'O "O' row, column, or diagonal, if any, will in the third sub can be connected into the space (i.e., three points in a line, win) of the straight line. If there are even a line but useless space, for step c.
c, judgment is not a two 'X "X' in the row, column, or diagonal lines excel, and the third empty blank. In this space (ie the player intercept) if the child will be next. If useless, for step d.
d, in the range of the lower sub random.
<3> print board, the coordinates of the lower sub-computer with 'O' instead.
5, winning or losing is determined
when it is determined that there are rows, columns, or diagonal 'X' 'O' into three lines, the output is determined (Congratulations, you win) (sorry, you lose) and out of the game appears diagonal, if iterate found not to meet the above requirements, and there is no data '' (space) (ie, under the full board), output (draw) and quit the game.
6, the logic
to start the game - select Computer upper hand - (ComputerGo - PrintfGame - IsWin - PlayGo - PrintfGame - IsWin) ( cycle to achieve)
To start the game - select players the upper hand --PrintfGame - (PlayGo - PrintfGame - IsWin - ComputerGo - PrintfGame - IsWin) (cycle to achieve)
Second, the source code
1, game.h (headers)
_CRT_SECURE_NO_WARNINGS. 1 #define #ifndef __game_h__ #define __game_h__ #define identifier defines the ROW line. 3 //. 3 = the ROW #define identifier defines COL. 3 //. 3 columns = COL void InitGame (ARR char [the ROW] [COL], int Row , int col); // initialize void PrintfGame (char arr [ROW] [COL], int row, int col); // print void menu (); // menu void ComputerGo (char arr [ROW] [COL], int row, int col); // computer walk void PlayGo (char arr [ROW] [COL], int row, int col); // players go char IsWin (char arr [ROW] [COL], int row, int col); // lose, win, and local #endif // __ game_h__
2, game.c (function definition)
_CRT_SECURE_NO_WARNINGS. 1 #define #include "game.h" // custom header file referenced #include <stdio.h> #include <time.h> #include <stdlib.h> void Menu () { the printf ( "*** ******************************* \ the n-"); printf (" ********* 1. ********* Paly \ the n-"); printf (" ********* ********* 0.exit \ the n-"); printf (" *** ******************************* \ n-"); } void InitGame (ARR char [the ROW] [COL], int row, int col) // initialize an array { int I = 0; int J = 0; for (I = 0; I <the ROW; I ++) { for (J = 0; J <COL; J ++) { ARR [I] [j] = ''; // use loop array elements are initialized to '(space) } } } void PrintfGame (char arr [ROW] [COL], int row, int col) // print board { // checkerboard print using a circulating int I = 0; int J = 0; for (I = 0; I <the ROW; I ++ ) // define the scope not exceeding the rOW { IF (I <the rOW -. 1) to print two lines before // { for (J = 0; J <COL; J ++) { IF (J <COL -. 1) before printing // two { the printf ( "% C |", ARR [I] [J]); } the else { the printf ( "% C", ARR [I] [J]); // print the third column } } the printf ( " \ n "); // output in the form of" C% |% C |% C " for (J = 0; J <COL; J ++) { IF (J <COL -. 1) { the printf (" --- | " );// print the first two columns } the else { printf ( "---"); // print the third column } } printf ( "\ the n-"); // output in the form of "--- | --- | ---" } the else // third printing OK { for (J = 0; J <COL; J ++) { IF (J <COL -. 1) { the printf ( "% C |", ARR [I] [J]); } the else { the printf ( "% C" , ARR [I] [J]); } } the printf ( "\ n-"); // output in the form of "C% |% C |% C" } } } void PlayGo (ARR char [the ROW] [COL], int row, int col) // players walk { int I = 0; int J = 0; int SET = 0; do { printf("\n"); the printf ( "Please enter the coordinates:> "); Scanf ( "% D% D", & I, & J); ; the printf ( "\ n-") the first input // pair i, j is decremented by 1, the user input when the input can be directly (1 1) point i--; J,; IF ((I <0) || (I> = Row) || (J <0) || (J> = COL) || (ARR [I] [J]! = '')) {// out of range input or coordinate input has pieces printf () "input error \ n-!"; SET =. 1; } the else { ARR [I] [J] = 'X-'; // lower sub player SET = 0; } } the while (SET); // not to enter the lower sub-cycle success success } char IsWin (ARR char [the ROW] [COL], Row int, int COL) determining winners and losers // { int 0 = I; int J = 0; int COUNT = 0; for (I = 0; I <the ROW; I ++) { IF ((ARR [I] [0] == ARR [I] [. 1]) &&(arr[i][1] == arr[i][2]) && (arr[i][0] != ' ')) { return 'X'; // produce a line row, returns 'X-' } } for (I = 0; I <the ROW; I ++) { IF ((ARR [0] [I] == ARR [. 1] [I ]) && (ARR [. 1] [I] == ARR [2] [I]) && (ARR [0] [I] = ''))! { return ''; // produce lash line, returns' ' } } IF ((ARR [0] [0] == ARR [. 1] [. 1]) && (ARR [. 1] [. 1] == ARR [2] [2]) && (ARR [. 1] [. 1 ]! = '') || (ARR [2] [0] == ARR [. 1] [. 1]) && (ARR [. 1] [. 1] == ARR [0] [2]) && (ARR [. 1 !] [. 1] = '')) { return 'O'; // to produce a diagonal line, returns 'O' } IF ((ARR [0] [0] =! '') && (arr[0][1] != ' ') && (arr[0][2] != ' ') && (arr[1][0] != ' ') && (arr[1][1] != ' ') && (arr[1][2] != ' ') && ! (ARR [2] [0] = '') && (ARR [2] [. 1 !] = ''!) && (ARR [2] [2] = '')) { return 'H'; // board has still not at full outcome generation, returns 'H' } Return 0; } void ComputerGo (ARR char [the ROW] [COL], Row int, int COL) // PC go { 0 = I int; int J = 0; int FLAG0 = 0; // flage0 Once there is a limit for judging success, it is no longer determined other int flag2 = 0; // flage2 when the computer is about to win, after produce different judgments into different Case int a case where Flag3 = 0; // flage3 for when a player is about to win, after produce different judgments into different Case printf ( "computer walk: \ the n-\ the n-"); // short of a computer on the sub-win situation IF (== 0 FLAG0) { for (I = 0; I <the rOW; I ++) // each line has two 'O''O' together, in the first at the lower sub-three spaces { IF ((ARR [I] [0] == ARR [I] [. 1] && ARR [I] [0] == 'O' && ARR [I] [2]! = 'X-' ) IF (== 0 FLAG0) { || (ARR [I] [. 1] == ARR [I] [2] &&arr[i][1] == 'O'&&arr[i][0] != 'X') || (ARR [I] [0] == ARR [I] [2] && ARR [I] [0] == 'O' && ARR [I] [. 1]! = 'X-')) { FLAG0. 1 = ; FLAG2 =. 1; BREAK; } } } for (j = 0; j < ROW; j ++) // for each column there are two 'O''O' together, the spaces in the third lower sub { IF ((ARR [0] [J] == ARR [. 1] [J] && ARR [0] [J] == 'O' && ARR [2] [J]! = 'X-') || (ARR [. 1] [J] == ARR [2] [ J] && ARR [. 1] [J] == 'O' && ARR [0] [J]! = 'X-') || (ARR [0] [J] == ARR [2] [J] && ARR [ 0] [J] == 'O' && ARR [. 1] [J] = 'X-'))! { FLAG2 = 2; FLAG0 =. 1; BREAK; } } } IF ((ARR [0] [0] == arr [1] [1] && arr [0] [0] == 'O' && arr [2] [2]! = 'X') // first diagonal || (arr [1] [1 ] == ARR [2] [2] && ARR [. 1] [. 1] == 'O' && ARR [0] [0]! = 'X-') || (ARR [0] [0] == ARR [ 2] [2] &&arr[0][0] == 'O'&&arr[1][1] != 'X') && (flag0 == 0)) { flag2 = 3; flag0 = 1; } if ((arr[0][2] == arr[1][1] && arr[0][2] == 'O'&&arr[2][0] != 'X')//第二条斜对角 || (arr[1][1] == arr[2][0] && arr[1][1] == 'O'&&arr[0][2] != 'X') || (arr[0][2] == arr[2][0] && arr[0][2] == 'O'&&arr[1][1] != 'X') && (flag0 == 0)) { flag2 = 4; flag0 = 1; } switch (flag2) { case 1: do { j = rand() % 3;// fixed line but changing the column, so that the blank piece into the BREAK; ARR [I] [J] = 'O'; { IF (ARR [I] [J] == '') } } the while (. 1); BREAK; Case 2: do {// RAND ()%. 3 generates a random number between 2. 1 0 I = RAND (. 3%); // fixed column but changing the line, so that the pieces enter the space if (arr [i] [j ] == '') { ARR [I] [J] = 'O'; BREAK; } } the while (. 1); BREAK; Case. 3: do {// changing ranks, but the limit (line number = column number), so that in the first diagonal down sub-blanks I = RAND (). 3%; J = RAND (). 3%; IF ((J == I) && (ARR [I] [J] == '')) { ARR [I] [J] = 'O'; BREAK; } } the while (. 1); BREAK; Case. 4: do {// changing ranks, but limits the number of rows and columns, so that the second ramp-down sub-blanks diagonally i RAND = (). 3%; J = RAND (). 3%; IF ((J == I) && (ARR [I] [J] == '') && (I ==. 1) || (J + I == 2) && (ARR [ I] [J] == '') || (I + J == 2) && (ARR [I] [J] == '')) { Arr [i] [j] = 'O'? BREAK; } } the while (. 1); BREAK; } // sub intercept a worse player won the game, the computer IF (== 0 FLAG0) { for (I = 0; I <the rOW; I ++) // row each have two 'X' ' X 'together, the spaces in the third lower sub { IF ((ARR [I] [0] == ARR [I] [. 1] && ARR [I] [0] ==' X '&& ARR [I ] [2]! = 'O') || (ARR [I] [. 1] == ARR [I] [2] && ARR [I] [. 1] == 'X-' && ARR [I] [0]! = 'O') || (ARR [I] [0] == ARR [I] [2] && ARR [I] [0] == 'X-' &&arr[i][1] != 'O')) { flag3 = 1; flag0 = 1; break; } } } IF (== 0 FLAG0) { for (J = 0; J <the ROW; J ++) // each column has two 'X''X' together, the third lower sub-space in at { IF ((ARR [0] [J] == ARR [. 1] [J] &&arr[0][j] == 'X'&&arr[2][j] != 'O') || (ARR [. 1] [J] == ARR [2] [J] && ARR [. 1] [J] == 'X-' && ARR [0] [J]! = 'O') || (ARR [0] [J] == ARR [2] [J] && ARR [0] [J] == 'X-' && ARR [. 1] [J] = 'O'))! { Flag3 = 2; FLAG0 =. 1; BREAK; } } } IF ((ARR [0] [0] == ARR [. 1] [. 1] && ARR [0] [0] == 'X-' && ARR [2] [2]! = 'O') // diagonal || (ARR [. 1] [. 1] == ARR [2] [2] && ARR [. 1] [. 1] == 'X-' && ARR [0] [0]! = 'O') || (arr [0] [0] == arr [2] [2] && arr [0] [0] == 'X' && arr [1] [1]! = 'O') && (flag0 == 0)) { flag3 = 3; flag0 = 1; } if ((arr[0][2] == arr[1][1] && arr[0][2] == 'X'&&arr[2][0] != 'O')//斜对角 || (arr[1][1] == arr[2][0] && arr[1][1] == 'X'&&arr[0][2] != 'O') || (arr[0][2] == arr[2][0] && arr[0][2] == 'X'&&arr[1][1] != 'O') && (flag0 == 0)) { flag3 = 4; flag0 = 1; } switch (flag3) { case 1: do { j = rand() % 3; if (arr[i][j] == ' ') { arr[i][j] = 'O'; break; } } while (1); break; case 2: do { i = rand() % 3; if (arr[i][j] == ' ') { arr[i][j] = 'O'; break; } } while (1); break; case 3: do do { i = rand() % 3; j = rand() % 3; if ((i == j) && (arr[i][j] == ' ')) { arr[i][j] = 'O'; break; } } while (1); break; case 4: do { i = rand() % 3; j = rand() % 3; if ((i == j) && (arr[i][j] == ' ') && (i == 1) || (i == j + 2) && (arr[i][j] == ' ') || (j == i + 2) && (arr[i][j] == ' ')) { arr[i][j] = 'O'; break; } } while (1); break; J = RAND (). 3%; I = RAND (). 3%; { { IF (== 0 FLAG0) // whether the player or the computer, without coming into the case of three sub-line, the computer generates a random piece } if (arr[i][j] == ' ') { arr[i][j] = 'O'; break; } } while (1); } }
3, text.c (logic and call)
_CRT_SECURE_NO_WARNINGS. 1 #define #define _CRT_SECURE_NO_WARNINGS. 1 #include "game.h" #include <stdio.h> #include <stdlib.h> #include <time.h> void Game1 (ARR char [the ROW] [COL], int Row , int col) // computer upper hand { char i = 0; the while (1) // only players to win or win, or computer board is full and we have to exit the loop { printf ( "\ the n-"); ComputerGo (arr, Row, COL) ; // computer down PrintfGame (arr, row, col) ; // print board i = IsWin (arr, row, col); // determines whether winning or losing if ((i == 'X' ) || (i = = 'O') || (I == '')) {// the computer after Now, there are three child into line, the computer must win satisfying printf ( "! computer win \ n-"); the printf ( "\ n-"); BREAK; } the else IF (I == 'H') // full board { printf ( "tie \ n!"); printf ( "\ the n-"); BREAK; } PlayGo (ARR, Row, COL); // players walk PrintfGame (arr, row, col) ; // print board i = IsWin (arr, row, col); // winning or losing is determined if ((i == 'X-') || (I == 'O') || (I == '')) {// the player after Now, meet into three sub-lines, the player must win printf ( "player win! \ n") ; the printf ( "\ n-"); BREAK; } the else IF (I == 'H') is full board // { the printf ( "tie \ n-!"); the printf ( "\ n-"); BREAK; } } } void Game2 (ARR char [the ROW] [COL], Row int, int COL) // player sente { char I = 0; int tmp = 0; the while (. 1) { IF (tmp == 0) // player printing a blank chess board before, is performed only once { printf("\n"); PrintfGame(arr, row, col); tmp = 1; } PlayGo(arr, row, col); PrintfGame(arr, row, col); printf("\n"); i = IsWin(arr, row, col); if ((i == 'X') || (i == 'O') || (i == ' ')) { printf("玩家赢!\n"); printf("\n"); break; } else if (i == 'H') { printf("和局!\n"); printf("\n"); break; } ComputerGo(arr, row, col); PrintfGame(arr, row, col); i = IsWin(arr, row, col); if ((i == 'X') || (i == 'O') || (i == ' ')) Printf ( "Computer Win \ n!"); { the printf ( "\ n-"); { break; } IF the else (I == 'H') { the printf ( "tie \ n-!"); the printf ( "\ n-"); BREAK; } } } // main function int main () { int I = 0; int 0 = J; int INPUT = 0; int tmp = 0; int. 3 = Row; int = COL. 3; char ARR [. 3] [. 3]; srand ((unsigned int) Time (NULL)); // random number generator time for generating a random number, not the same every do { Menu (); // Print printf ( "Please choose:> \ n-"); Scanf ( "% D", & INPUT); printf ( "\ n-"); Switch (INPUT) { Case. 1: InitGame (ARR, Row, COL); // initialize function do printf ( "Please choose:> \ n1 player computer 2. sente sente \ n-."); Scanf ( "% D", & tmp); Switch (tmp) { Case. 1: Game1 (ARR, Row, COL); // computer upper hand tmp = 0; BREAK; Case 2: Game2 (arr, Row, COL); // players just get tmp = 0; BREAK; default: printf ( "! make a mistake please re-enter:> \ n \ n") ; BREAK; } } the while (tmp); BREAK; Case 0: printf ( "game exit \ the n-!"); BREAK; default: printf ( "make a mistake \ the n-!"); BREAK; } } the while (the iNPUT) ; System ( "PAUSE"); return 0; }
It is more than the entire contents of this article, Thanks for watching!