Design Title: Introduction to Tingzi Game Design and Implementation: A tandem game in Tingzi grid in which two players representing O and X alternately fall in the grid. Any three players form a straight line to win. Today, Xiao Ming invented a new game: 1) The size of the chessboard 3x3, the winning conditions remain unchanged; (2) If a party falls the fourth child and still does not win, the first child falls will be removed, and so on.
There are many similar articles, but fewer can be taken away, so this article focuses on the part of taking away the pieces.
header file
#include <stdio.h> #include <windows.h> #Include <stdlib. H>//for generating random numbers //global variable int QP[3][3];//Chess board, 1 for and 2 for ×, 0 is empty int qp1[8] = { 0 }, qp2[8] = { 0 };//Used to store the number for every six steps of extinction int player=1;//1 for player, 2 for × Player 2 or Computer int cnt1=0,cnt2=0; int judge_win();//Judging Win or Lose void gotoxy(int x, int y);//Jump Cursor void print_menu();//menu void print_box();//Checkerboard void move_player1();//_Chess void move_player2(int oder);//× Play chess int game(int oder);//Game Body void turn();//Player Status void updatebox();//Update the board void xiaozi();//Remove a piece
Principal function
int main() { int oder; system("mode con cols=90 lines=35"); begin: print_menu(); scanf("%d", &oder); for (int a = 0; a < 3; a++) for (int b = 0; b < 3; b++) QP[a][b] = 0;//Initialize a,b switch (oder) { case 0: return 0; break; case 1: game(oder); break; case 2: game(oder); break; default: printf("Input Error Return Menu\n"); goto begin; } return 0; }
Take away the pieces:
Principle: Define two arrays to store the coordinates of the chess pieces played (qb1 and qp2), then count them with cnt. When step 4 is reached (cnt=8 because of the one-dimensional array storage), assign the coordinates of the first chess piece to 0 (meaning there is no chess here), update the chess board, and use% to update the array of coordinates of the chess played (referring to QB1 and qp2)
void xiaozi() { if (cnt1 >= 8) { QP[qp1[(cnt1) %8]][qp1[((cnt1)%8+1)]] = 0; updatebox(); } if (cnt2 >= 8) { QP[qp2[(cnt2) % 8]][qp2[((cnt2) % 8 + 1)]] = 0; updatebox(); } }
opening menu
void print_menu() { printf("Please enter:\n1 Player vs. Player\n2 Ordinary human\n0 Exit Game\n"); }
Print board
void print_box() { gotoxy(26, 5); printf(" ┃ ┃ "); gotoxy(26, 6); printf(" ┃ ┃ "); gotoxy(26, 7); printf(" ┃ ┃ "); gotoxy(26, 8); printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━"); gotoxy(26, 9); printf(" ┃ ┃ "); gotoxy(26, 10); printf(" ┃ ┃ "); gotoxy(26, 11); printf(" ┃ ┃ "); gotoxy(26, 12); printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━"); gotoxy(26, 13); printf(" ┃ ┃ "); gotoxy(26, 14); printf(" ┃ ┃ "); gotoxy(26, 15); printf(" ┃ ┃ \n"); }
Player 1 Move
void move_player1() { do { gotoxy(60, 15); printf(" "); gotoxy(60, 6); printf("Please enter x Axis coordinates: "); gotoxy(60, 7); printf("Please enter y Axis coordinates: "); gotoxy(60, 6); int x, y; printf("Please enter x Axis coordinates:"); scanf("%d", &x); gotoxy(60, 7); printf("Please enter y Axis coordinates:"); scanf("%d", &y); if ((x > 3) || (x < 0) || (y < 0) || (y > 3)) { gotoxy(60, 15); printf("x,y Value of 1 to 3, please retry"); continue; } if (QP[x - 1][y - 1] != 0) { gotoxy(60,15); printf("There are pieces here. Please try again"); continue; } else { QP[x - 1][y - 1] = 1; qp1[cnt1 %8] = x - 1; qp1[(cnt1+1)%8] = y - 1; break; } } while (1); updatebox(); player =
Player 2
void move_player2(int oder) { if (oder == 1) //Game player { do { gotoxy(60, 15); printf(" "); gotoxy(60, 6); printf("Please enter x Axis coordinates: "); gotoxy(60, 7); printf("Please enter y Axis coordinates: "); gotoxy(60, 6); int x, y; printf("Please enter x Axis coordinates:"); scanf("%d", &x); gotoxy(60, 7); printf("Please enter y Axis coordinates:"); scanf("%d", &y); if ((x > 3) || (x < 0) || (y < 0) || (y > 3)) { gotoxy(60, 15); printf("x,y Value of 1 to 3, please retry"); continue; } if (QP[x - 1][y - 1] != 0) { gotoxy(60, 15); printf("There are pieces here. Please try again"); continue; } else { QP[x - 1][y - 1] = 2; qp2[cnt2 % 8] = x - 1; qp2[(cnt2 + 1) % 8] = y - 1; break; } } while (1); }else if(oder==2) { while (1) { int x = rand() % 3; int y = rand() % 3; if (QP[x][y] == 0) { QP[x][y] = 2; break; } } } updatebox(); player = 1; }
Gameplay
int game(int oder) { print_box(); do { turn();//To whom move_player1(); judge_win(); if (judge_win() != 0) break; cnt1 += 2; xiaozi(); turn(); move_player2(oder); judge_win(); if (judge_win() != 0) break; cnt2 += 2; xiaozi(); } while (1); if (judge_win ()==1 ) MessageBox(NULL, TEXT("√Win"), TEXT("Game over"), 0); else if(judge_win ()==2) MessageBox(NULL, TEXT("×Win"), TEXT("Game over"), 0); return 0; }
Print Game Dynamics
void turn() { gotoxy(60, 5); printf(" "); gotoxy(60, 5); if(player==1) printf("√"); else if(player == 2) printf("×"); gotoxy(63, 5); printf("go"); }
Update the board
void updatebox() { int a, b;//Used to calculate the position of printed chess pieces for(a=0;a<3;a++) for (b = 0; b < 3; b++) { if (QP[a][b] == 1) { gotoxy(30 + a * 8, 6 + b * 4); printf("√"); }else if (QP[a][b] == 2) { gotoxy(30 + a * 8, 6 + b * 4); printf("×"); } else if (QP[a][b] == 0) { gotoxy(30 + a * 8, 6 + b * 4); printf(" "); } } }
Judging Win/Lose
int judge_win() { for (int r = 0; r < 3; r++) { //Win returns 1, × Win returns 2 //Judgment of Three Horizons and Three Verticals if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 1 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 1) return 1; if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 2 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 2) return 2; } if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==1 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] ==1) return 1; if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==2 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] == 2) return 2; return 0; }
Move Cursor
void gotoxy(int x, int y) { COORD pos = { x,y }; HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hout, pos);//Set cursor position and move to corresponding position }//
Total Code
#include <stdio.h> #include <windows.h> #Include <stdlib. H>//for generating random numbers //global variable int QP[3][3];//Chess board, 1 for and 2 for ×, 0 is empty int qp1[8] = { 0 }, qp2[8] = { 0 };//Used to store the number for every six steps of extinction int player=1;//1 for player, 2 for × Player 2 or Computer int cnt1=0,cnt2=0; int judge_win();//Judging Win or Lose void gotoxy(int x, int y);//Jump Cursor void print_menu();//menu void print_box();//Checkerboard void move_player1();//_Chess void move_player2(int oder);//× Play chess int game(int oder);//Game Body void turn();//Player Status void updatebox();//Update the board void xiaozi();//Remove a piece int main() { int oder; system("mode con cols=90 lines=35"); begin: print_menu(); scanf("%d", &oder); for (int a = 0; a < 3; a++) for (int b = 0; b < 3; b++) QP[a][b] = 0;//Initialize a,b switch (oder) { case 0: return 0; break; case 1: game(oder); break; case 2: game(oder); break; default: printf("Input Error Return Menu\n"); goto begin; } return 0; } void print_menu() { printf("Please enter:\n1 Player vs. Player\n2 Ordinary human\n0 Exit Game\n"); } void print_box() { gotoxy(26, 5); printf(" ┃ ┃ "); gotoxy(26, 6); printf(" ┃ ┃ "); gotoxy(26, 7); printf(" ┃ ┃ "); gotoxy(26, 8); printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━"); gotoxy(26, 9); printf(" ┃ ┃ "); gotoxy(26, 10); printf(" ┃ ┃ "); gotoxy(26, 11); printf(" ┃ ┃ "); gotoxy(26, 12); printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━"); gotoxy(26, 13); printf(" ┃ ┃ "); gotoxy(26, 14); printf(" ┃ ┃ "); gotoxy(26, 15); printf(" ┃ ┃ \n"); } void move_player1() { do { gotoxy(60, 15); printf(" "); gotoxy(60, 6); printf("Please enter x Axis coordinates: "); gotoxy(60, 7); printf("Please enter y Axis coordinates: "); gotoxy(60, 6); int x, y; printf("Please enter x Axis coordinates:"); scanf("%d", &x); gotoxy(60, 7); printf("Please enter y Axis coordinates:"); scanf("%d", &y); if ((x > 3) || (x < 0) || (y < 0) || (y > 3)) { gotoxy(60, 15); printf("x,y Value of 1 to 3, please retry"); continue; } if (QP[x - 1][y - 1] != 0) { gotoxy(60,15); printf("There are pieces here. Please try again"); continue; } else { QP[x - 1][y - 1] = 1; qp1[cnt1 %8] = x - 1; qp1[(cnt1+1)%8] = y - 1; break; } } while (1); updatebox(); player = 2; } void move_player2(int oder) { if (oder == 1) //Game player { do { gotoxy(60, 15); printf(" "); gotoxy(60, 6); printf("Please enter x Axis coordinates: "); gotoxy(60, 7); printf("Please enter y Axis coordinates: "); gotoxy(60, 6); int x, y; printf("Please enter x Axis coordinates:"); scanf("%d", &x); gotoxy(60, 7); printf("Please enter y Axis coordinates:"); scanf("%d", &y); if ((x > 3) || (x < 0) || (y < 0) || (y > 3)) { gotoxy(60, 15); printf("x,y Value of 1 to 3, please retry"); continue; } if (QP[x - 1][y - 1] != 0) { gotoxy(60, 15); printf("There are pieces here. Please try again"); continue; } else { QP[x - 1][y - 1] = 2; qp2[cnt2 % 8] = x - 1; qp2[(cnt2 + 1) % 8] = y - 1; break; } } while (1); }else if(oder==2) { while (1) { int x = rand() % 3; int y = rand() % 3; if (QP[x][y] == 0) { QP[x][y] = 2; break; } } } updatebox(); player = 1; } int game(int oder) { print_box(); do { turn();//To whom move_player1(); judge_win(); if (judge_win() != 0) break; cnt1 += 2; xiaozi(); turn(); move_player2(oder); judge_win(); if (judge_win() != 0) break; cnt2 += 2; xiaozi(); } while (1); if (judge_win ()==1 ) MessageBox(NULL, TEXT("√Win"), TEXT("Game over"), 0); else if(judge_win ()==2) MessageBox(NULL, TEXT("×Win"), TEXT("Game over"), 0); return 0; } void turn() { gotoxy(60, 5); printf(" "); gotoxy(60, 5); if(player==1) printf("√"); else if(player == 2) printf("×"); gotoxy(63, 5); printf("go"); } void updatebox() { int a, b;//Used to calculate the position of printed chess pieces for(a=0;a<3;a++) for (b = 0; b < 3; b++) { if (QP[a][b] == 1) { gotoxy(30 + a * 8, 6 + b * 4); printf("√"); }else if (QP[a][b] == 2) { gotoxy(30 + a * 8, 6 + b * 4); printf("×"); } else if (QP[a][b] == 0) { gotoxy(30 + a * 8, 6 + b * 4); printf(" "); } } } void xiaozi() { if (cnt1 >= 8) { QP[qp1[(cnt1) %8]][qp1[((cnt1)%8+1)]] = 0; updatebox(); } if (cnt2 >= 8) { QP[qp2[(cnt2) % 8]][qp2[((cnt2) % 8 + 1)]] = 0; updatebox(); } } int judge_win() { for (int r = 0; r < 3; r++) { //Win returns 1, × Win returns 2 //Judgment of Three Horizons and Three Verticals if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 1 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 1) return 1; if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 2 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 2) return 2; } if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==1 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] ==1) return 1; if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==2 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] == 2) return 2; return 0; } void gotoxy(int x, int y) { COORD pos = { x,y }; HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hout, pos);//Set cursor position, move to }//
There is a print bug in this code and two pieces of text cannot be printed
This is the first-year homework of Zhang fm, a student of sztu, which should be handed over to Mr. s Xiaole. In order to prevent similar homework, this statement is made that in order to complete the blog task without being curled up, only a few posts can be sent out to make up the number...........
Code reference: https://b23.tv/9dCyt4B
Implement if you want to achieve AI reference (none in this code): (27 messages) Implementation of artificial intelligence algorithm for Tingzi/Sanzi DanteKing's Blog - CSDN Blog_ Tingzi ai algorithm