Realize the game of three-bang and the problem-solving idea in c language

The game
idea of ​​Sanbang is as follows:
**1,** first print the menu and tell the player how to start the game. If you enter 1, the game starts. If you enter 0, the game ends. .
**2, **Initialize the board so that the board is blank.
**3,** Print the chessboard.
**4,** Then use the while loop to continue the game, first the player plays chess, and then the computer plays chess, each time you play chess, you must print the chessboard and judge whether you win or lose.
**5,** Until someone wins or the board is full, the game is over.

#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//Print menu
void Menu()
{ printf("********************************\n"); printf(" Welcome to the game of backgammon \n"); printf(" Enter 1: enter the game \n"); printf(" Enter 0: exit the game \n"); printf("********** ********************\n"); }





//初始化棋盘
void InitBoard(char board[][COL], int row, int col)
{ int i = 0; int j = 0; for (i = 0;i < row;i++) { for (j = 0;j < col;j++) { board[i][j] = ’ '; } } }









//打印棋盘
void ShowBoard(char board[][COL], int row, int col)
{ int i = 0; int j = 0; for (i = 0;i < row;i++) { for (j = 0;j < col;j++) { printf(" %c “, board[i][j]);//打印三个空格 if (j < col - 1) { printf(”|"); } } printf("\n"); if (i < row - 1) { for (j = 0;j < col;j++) { printf("—"); if (j < col - 1) { printf("|"); } } } printf("\n"); } }


























//The player moves, the pawn is 'O'
void PlayerMove(char board[][COL], int row, int col)
{ int x = 0; int y = 0; while (1) { printf("Player move: \ n"); printf("Please enter the coordinates of 1->3:"); scanf("%d%d", &x, &y); if (x > 0 && x <= 3 && y > 0 && y <= 3) { if (board[x - 1][y - 1] == ' ')//The value input by the player, we need to subtract one to correspond to the position corresponding to the array { board[x - 1] [y - 1] = 'O'; break; } else { printf("The coordinates you entered already have pawns, please re-enter:"); } } else { printf("The coordinates you entered are incorrect! Please re-enter :"); } } }
























//Determine whether the board is full
int IsFull(char board[][COL], int row, int col)
{ int i = 0; int j = 0; for (i = 0;i < ROW;i++) { for (j = 0;j < COL;j++) { if (board[i][j] == ' ') { return 0; } } } return 1; }













//Judgment to win
int IsWin(char board[][COL], int row, int col)
{ int i = 0; for (i = 0;i < ROW;i++) { //Judgment whether there are three identical horizontal Pieces if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } //Determine whether there are three identical pieces in the vertical direction if (board[0][i] ==board[1][i] && board[1][i] == board [2][i] && board[0][i] != ' ') { return board[0][i]; } //Determine whether there are three identical pieces on the diagonal from the upper left corner to the lower right corner if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { return board [0][0]; } //Determine whether there are three identical pieces on the diagonal from the upper right corner to the lower left corner



















if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != ’ ')
{ return board[i][0]; } if (IsFull(board, 3, 3) == 1) { return ‘P’;//平局 } } return ‘C’;//继续下棋 }








//The computer moves, the pawn is 'X'
void ComputerMove(char board[][COL], int row, int col)
{ int x = 0; int y = 0; printf("computer move:\n"); while (1) { x = rand() % ROW; y = rand() % COL; if (board[x][y] == ' ') { board[x][y] = 'X'; break; } } }













void Game()
{ int a = 0; char board[ROW][COL] = { 0 }; //Initialize the board InitBoard(board, 3, 3); //Print the board ShowBoard(board, 3, 3); while (1) { //The player moves first PlayerMove(board, 3, 3); //Print the chessboard after moving ShowBoard(board, 3, 3); //Judging that the computer or the player has three consecutive pieces a = IsWin(board , 3, 3); if (a != 'C')//C means that there are no three first arrivals { break; } //The computer moves ComputerMove(board, 3, 3); //Continue to print the board ShowBoard( board, 3, 3); } if (a == 'O')//return is O player wins { printf("Congratulations you won!"); } else if (a == 'X')/ /return is X computer wins {





























printf("It's a pity, you lost!");
}
else if (a == 'P')//If you return P and you don't win, a draw
{ printf("A draw!"); } }


int main()
{ int input = 0; srand((unsigned int)time(NULL)); do { //print menu Menu(); printf("Please enter your choice:"); scanf("%d" , &input); switch (input) { case 1: //If input 1, start the game Game(); break; case 0: //If input 0, end the game printf("Exit the game\n"); break; default :// is neither 1 nor 0, prompting an input error printf("Your input is wrong! Please re-enter:"); break; } } while (input); system("pause"); return 0; }

























Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324373223&siteId=291194637