用了这好几天的时间在闲暇时间写了一个小时候玩的井字棋,或者叫三子棋的小游戏,来娱乐娱乐,虽然是黑白框,并且电脑有点傻之外,一切良好。
首先写之前就要先明白思路,这个棋盘要放到一个二维数组里面这样才能更好的显示出来,并且方便操作,先打印出来棋盘这是必须的,这就是基本的二维数组的打印就好,把棋盘初始化为空格就好了,然后等到走的时候,拿相应的字符来代替就好了;电脑走的话就用随机数来差生坐标就好了,所以才说它傻;
先上代码:
头文件 szq.h
<pre name="code" class="cpp">#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __SZQ_GAME_H__
#define __SZQ_GAME_H__
#define ROWS 3
#define COLS 3
void init_board(char board[ROWS][COLS], int rows, int cols);
void print_board(char board[ROWS][COLS], int rows, int cols);
void play_game(char board[ROWS][COLS]);
char check_win(char board[ROWS][COLS]);
void player_move(char board[ROWS][COLS]);
void com_move(char board[ROWS][COLS]);
#endif
#define _CRT_SECURE_NO_WARNINGS 1
#include "szq.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void menu()
{
printf("*************************\n");
printf("***** 1.play ******\n");
printf("***** 0.exit ******\n");
printf("*************************\n");
}
void game()
{
int input = 1;
char board[ROWS][COLS] = { 0 };
init_board(board, ROWS, COLS);
while (input)
{
menu();
printf("请选择:");
scanf("%d", &input);
if (input == 1)
{
init_board(board, ROWS, COLS);
print_board(board, ROWS, COLS);
play_game(board);
}
}
}
int main()
{
game();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "szq.h"
#include <stdio.h>
#include <stdlib.h>
void com_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
srand((unsigned)time(NULL));
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = 'O';
return;
}
}
}
void player_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:");
scanf("%d%d", &x, &y);
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
return;
}
else
{
printf("输入错误");
}
}
}
char check_win(char board[ROWS][COLS])
{
int i = 0;
for (i = 0; i < 3; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][1] != ' '))
{
return 'p';
}
}
for (i = 0; i < 3; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[1][i] != ' '))
{
return 'p';
}
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[1][1] != ' '))
{
return 'p';
}
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))
{
return 'p';
}
}
void init_board(char board[ROWS][COLS], int rows, int cols)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROWS][COLS], int rows, int cols)
{
int i = 0;
for (i = 0; i < ROWS; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
printf("---|---|---\n");
}
}
void play_game(char board[ROWS][COLS])
{
char ret;
int count = 0;
while (1)
{
if (count >= 9)
{
printf("平局!!!!\n");
break;
}
player_move(board);
count++;
print_board(board, ROWS, COLS);
if ((ret = check_win(board)) == 'p')
{
printf("你获胜了!!!!!\n");
break;
}
if (count >= 9)
{
printf("平局!!!!\n");
break;
}
count++;
printf("电脑走:\n");
com_move(board);
print_board(board, ROWS, COLS);
if ((ret = check_win(board)) == 'p')
{
printf("呵呵!!!\n");
break;
}
}
}
这样就结束了,其实原理不复杂,很好相通就是细节还是要注意,细心一点就好。