C语言实现三子棋,简单实现人机对战操作,代码包括,显示棋盘和刷新操作,用户输入棋盘坐标,来实现下棋操作,通过随机生成来实现与电脑对战,通过三子连珠的情况判断游戏结束。
通过完成三子棋代码,熟练掌握和运用二维数组,明白数组存储时的模型,加深数组传参时的条件以及数组如何传参。加强头文件和函数的运用。
头文件game.h声明
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<Windows.h>
#pragma warning(disable : 4996)
#define ROW 3 //定义行列
#define COL 3
void Game();
char Judge(char* broad, int i, int j);
void PlayerMove(char* broad, int i, int j);
void ComputerMove(char* broad, int i, int j);
void ShowBoard(char* broad, int i, int j);
int roll();
#endif
具体头文件内容,函数的定义 game.c
#include "game.h"
char Judge(char board[][COL], int row, int col)
{
int i = 0;
for (; i < row; i++){
if (board[i][0] != ' ' && board[i][0] == board[i][1] && \
board[i][1] == board[i][2]){
return board[i][0];
}
}
for (i = 0; i < col; i++){
if (board[0][i] != ' ' && board[0][i] == board[1][i] && \
board[1][i] == board[2][i]){
return board[0][i];
}
}
if (board[0][0] != ' ' &&board[0][0] == board[1][1] && \
board[1][1] == board[2][2]){
return board[1][1];
}
if (board[0][2] != ' ' &&board[0][2] == board[1][1] && \
board[1][1] == board[2][0]){
return board[1][1];
}
for (i = 0; i < row; i++){
int j = 0;
for (; j < col; j++){
if (board[i][j] == ' '){
return 'N';
}
}
}
return 'E';
}
void InitBoard(char board[][COL], int row, int col){
for (int i = 0; i < col; i++)
for (int j = 0; j < row; j++)
board[i][j] = ' ';
}
void Game()
{
char result = '\0';
char board[ROW][COL];
InitBoard(board, ROW, COL);
ShowBoard(board, ROW, COL);
srand((unsigned int)time(NULL));
if (roll() == 1){
while (1){
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
system("CLS");
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
}
}
else{
while (1){
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
system("CLS");
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
}
}
switch (result){
case 'X': //you win
break;
case 'O': //computer win
break;
case 'E': //Equal
break;
default:
printf("bug?!\n");
break;
}
//memset(board, ' ', sizeof(board));
printf("再来一把?\n");
}
void PlayerMove(char broad[][COL], int row, int col){
printf("输入X坐标与Y坐标");
int x = 0;
int y = 0;
while (1){
scanf("%d%d", &x, &y);
if (x >= row || x < 0 || y >= col || y < 0)
printf("输入有误,范围在(1,3)之间");
else if (broad[x][y] != ' ')
printf("输入的位置已被占用,请重新输入");
else{
broad[x][y] = 'X';
break;
}
}
}
void ShowBoard(char board[][COL], int raw, int col){
for (int i = 0; i < col; i++){
for (int j = 0; j < raw; j++)
printf("| %c ", board[i][j]);
printf("|\n");
printf("----------------\n");
}
}
int GetRandom(int start, int end)
{
return rand() % (end - start + 1) + start;
}
void ComputerMove(char board[][COL], int row, int col)
{
while (1){
int x = GetRandom(1, 3);
int y = GetRandom(1, 3);
if (board[x - 1][y - 1] == ' '){
board[x - 1][y - 1] = 'O';
break;
}
}
}
int roll(){
int x = 0;
printf("选择先手\n");
printf("1.玩家先手 2.电脑先手\n");
printf("请选择:");
scanf("%d", &x);
return x;
}
新增选择电脑或玩家先手的方法
int roll(){
int x = 0;
printf("选择先手\n");
printf("1.玩家先手 2.电脑先手\n");
printf("请选择:");
scanf("%d", &x);
return x;
}
//game()函数中的判定 调换PLAYER和COMPUTER位置
if (roll() == 1){
while (1){
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
system("CLS");
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
}
}
else{
while (1){
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
system("CLS");
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
}
主函数,实现菜单方法
#include "game.h"
void Showmenu(){
printf("#################################\n");
printf("## 1. Play 2. Exit ##\n");
printf("#################################\n");
printf("Please Select: ");
}
int main()
{
int select = 0;
int quit = 0;
while (!quit){
Showmenu();
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
printf("Bye bye!\n");
quit = 1;
break;
default:
printf("select error, try again!\n");
break;
}
}
system("pause");
return 0;
}
后续更新 优化判定算法,和电脑算法。