三子棋小游戏实现
来源:互联网 发布:nba2016数据 编辑:程序博客网 时间:2024/06/05 19:03
首先在写代码之前,得有整体的构思,包括棋盘的创建,玩家和电脑如何走,电脑随机数走在相当于二维数组的棋盘,如何判断谁赢谁输等一些问题,将三子棋游戏这个大问题,化为几个小部分,逐一完成。
接下来看看代码吧:
game.h
#ifndef __GAME_H__ #define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 3
#define COLS 3
void init_board(char board[ROWS][COLS], int row, int col);
void display_board(char board[ROWS][COLS], int row, int col);
void player_move(char board[ROWS][COLS], int row, int col);
void computer_move(char board[ROWS][COLS], int row, int col);
char check_win(char board[ROWS][COLS], int row, int col);
#endif
game.c
#include "game.h"
void init_board(char board[ROWS][COLS], int row, int col)
{
memset(board, ' ', col*row*sizeof(char));
}
void display_board(char board[ROWS][COLS], int row, int col)
{ int i = 0;
for(i=0; i<row; i++)
{ printf(" %c | %c | %c \n", board[i][0],board[i][1],board[i][2]);
if(i != 2)
printf("---|---|---\n"); }
}
void player_move(char board[ROWS][COLS], int row, int col)
{ int x = 0; int y = 0;
while(1)
{ printf("请输入坐标:>");
scanf("%d%d", &x, &y);
x--;
y--;
if(((x>=0)&&(x<=2))&&((y>=0)&&(y<=2)))
{ if(board[x][y] == ' ')
{ board[x][y] = 'X'; break;
}
else
{ printf("下标有误, 重新输入!");
}
}
else
{
printf("下标有误, 重新输入!");
}
}
}
void computer_move(char board[ROWS][COLS], int row, int col)
{
while(1)
{
int x = rand()%3;
int y = rand()%3;
if(board[x][y] == ' ')
{ board[x][y] = '0';
break;
}
}
}
static int is_full(char board[ROWS][COLS], 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; }
char check_win(char board[ROWS][COLS], int row, int col)
{ int i = 0;
for(i=0; i<row; i++)
{ if((board[i][0]==board[i][1]) &&(board[i][1]==board[i][2]) &&(board[i][1]!=' '))
return board[i][1]; }
for(i=0; i<col; i++)
{ if((board[0][i]==board[1][i]) &&(board[1][i]==board[2][i]) &&(board[1][i]!=' '))
return board[1][i]; }
if((board[0][0]==board[1][1]) &&(board[1][1]==board[2][2]) &&(board[1][1]!=' '))
return board[1][1];
if((board[0][2]==board[1][1]) &&(board[1][1]==board[2][0]) &&(board[1][1]!=' '))
return board[1][1];
if(is_full(board, row, col))
{ return 'q'; }
return ' '; }
test.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"
void game()
{ char board[ROWS][COLS];
char ret = 0;
init_board(board, ROWS, COLS);
display_board(board, ROWS, COLS);
srand((unsigned int)time(NULL));
//
while(1)
{ player_move(board, ROWS, COLS);
if((ret = check_win(board, ROWS, COLS)) != ' ')
break; display_board(board, ROWS, COLS);
computer_move(board, ROWS, COLS);
if((ret = check_win(board, ROWS, COLS)) != ' ')
break; display_board(board, ROWS, COLS); }
//
if(ret == 'X') { printf("玩家赢\n"); }
else if(ret == '0')
{ printf("呵呵\n"); }
else if(ret == 'q')
{ printf("平局\n"); }
//
display_board(board, ROWS, COLS); }
void menu()
{ printf("1.play\n");
printf("0.exit\n"); }
int main()
{ int input = 0;
do {
menu();
printf("请选择>:");
scanf("%d", &input);
switch(input)
{ case 1: game();
break;
case 0: break;
default:
printf("选择错误\n"); break; } }
while (input); return 0; }
代码终于写完了,很开心。
- 三子棋小游戏的实现
- 小游戏三子棋的实现
- 三子棋小游戏实现
- 小游戏之三子棋的实现
- C语言实现小游戏---三子棋
- 简单的小游戏---代码实现三子棋
- 用C语言实现三子棋小游戏
- C语言实现简易三子棋小游戏
- C语言实现简单的三子棋小游戏
- 小游戏三子棋的C语言代码实现
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 【小游戏】三子棋
- 三子棋小游戏
- Spring注解大全
- nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
- Jupyter Notebook打开时报错的问题解决办法
- ceph主要数据结构解析2-Rados.h
- memmove 和 memcpy的区别以及处理内存重叠问题
- 三子棋小游戏实现
- Java面试经典题目
- JQuery DataTables 服务端自定义查询(最新版本介绍)
- 扩展欧几里得--part2
- 【逆序对】【cofun1917】奇数码问题
- 【Direct3D】光照与材质
- 【Openjudge】由中根序列和后根序列重建二叉树
- github 下载修改上传使用
- Python3之Django Web框架分页多页码处理一