三子棋初稿
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三子棋初稿相关的知识,希望对你有一定的参考价值。
游戏规则:人机对战,在九宫格里下棋,O代表电脑所下棋子的地方,X代表玩家下棋的地方,哪一方先连成三个那边算赢。
程序要求:
- 电脑所下位的行列值由电脑随机生成。
- 玩家通过输入行列值进行下棋。
- 每走一步都要形成可视化界面,O代表电脑,X代表玩家。
- 要及时分出输赢或平局,分出胜负后,要 及时结束本局游戏。
- 游戏是否结束,由玩家决定。
头文件:
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef _THREE_CHESS_H_
#define _THREE_CHESS_H_
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <time.h>
#define ROW 3
#define COL 3
void ShowUI();
void Game();
void ComputerMove(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);
#endif
子函数
#define _CRT_SECURE_NO_WARNINGS 1
#include "three_chess.h"
void ShowUI()
{
printf("##########################\n");
printf("##1.PLAY 0.EXIT##\n");
printf("##########################\n");
printf("select number: \n");
}
void Game()
{
printf("Game bigin\n");
char board[ROW][COL];
char result = ‘N‘;
srand((unsigned long)time(NULL));
memset(board, ‘ ‘, sizeof(board)); //按字节初始化
while (1)
{
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if ( result!= ‘N‘) //结果‘X‘ ‘O‘ ‘E‘ ‘N‘
{
break;
}
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != ‘N‘) //结果‘X‘ ‘O‘ ‘E‘ ‘N‘
{
break;
}
}
switch (result){
case ‘X‘:
printf("congratulation!!!you win...");
break;
case ‘O‘:
printf("oh no~~~~~you lose");
break;
case ‘E‘:
printf("平局");
break;
default:
break;
}
}
void ComputerMove(char board[][COL], int row, int col)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ‘ ‘)
{
board[x][y] = ‘O‘;
break;
}
}
}
void PlayerMove(char board[][COL], int row, int col)
{
int x, y;
while (1)
{
printf("Please Enter Your Pose:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
if (board[x-1][y-1] == ‘ ‘)
{
board[x - 1][y - 1] = ‘X‘;
break;
}
else printf("Your pose has been filled , try agin");
}
else printf("Enter error , try agin");
}
}
char Judge(char board[ROW][COL], int row, int col)
{
int i ,j;
for (i=0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && //续行符
board[i][0] != ‘ ‘)
return board[i][0];
}
for (j=0; j <col; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && //续行符
board[0][j] != ‘ ‘)
return board[0][j];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && //续行符
board[0][0] != ‘ ‘)
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && //续行符
board[0][2] != ‘ ‘)
return board[0][2];
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ‘ ‘)
return ‘N‘;
}
}
return ‘E‘;
}
void ShowBoard(char board[][COL], int row, int col)
{
int i, j;
printf(" 1 2 3 \n");
printf("----------\n");
for (i = 0; i <row; i++)
{
printf("%d|", i+1);
for (j = 0; j < col; j++)
{
printf("%c|", board[i][j]);
}
printf("\n");
}
}
主程序:
#include "three_chess.h"
int main()
{
int select;
ShowUI();
do{
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 0:
printf("bye!!\n");
break;
default:
printf("please printf agin:");
break;
}
} while(select);
system("pause");
return 0;
}
以上是关于三子棋初稿的主要内容,如果未能解决你的问题,请参考以下文章