三子棋
来源:互联网 发布:js设置input文本颜色 编辑:程序博客网 时间:2024/06/07 16:37
首先定义一个头文件game.h
在里面定义宏和声明游戏实现所用到的函数
#ifndef __GAME_H__#define __GAME_H__#include <stdlib.h>#include <time.h>#define ROWS 3#define COLS 3void InitBoard(char board[ROWS][COLS], int rows, int cols);void DisplayBoard(char board[ROWS][COLS], int rows, int cols);void ComputerMove(char board[ROWS][COLS], int rows, int cols);void PlayerMove(char board[ROWS][COLS], int rows, int cols);char CheakWin(char board[ROWS][COLS], int rows, int cols);#endif//__GAME_H__
接着写主函数,测试函数,菜单以及游戏实现函数
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include "game.h"void menu(){ printf("*******************************************\n"); printf("******** 1.play 0.exit **********\n"); printf("*******************************************\n");}void game(){ char win = 0; char arr[ROWS][COLS] = { 0 }; InitBoard(arr,ROWS,COLS); DisplayBoard(arr,ROWS,COLS); while (1) { ComputerMove(arr, ROWS, COLS); win=CheakWin(arr, ROWS, COLS); if (win != ' ') { break; } DisplayBoard(arr, ROWS, COLS); PlayerMove(arr, ROWS, COLS); win = CheakWin(arr, ROWS, COLS); DisplayBoard(arr, ROWS, COLS); if (win != ' ') { break; } } if (win == 'X') { printf("computer win!\n"); } else if (win == '*') { printf("player win!\n"); } else if (win == 'q') { printf("平局\n"); }}void test(){ int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("please choose:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("input errr!please input again!\n"); break; } } while (input);}int main(){ test(); return 0;}
最后新建一个文件来写实现游戏的相关函数
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include "game.h"void InitBoard(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 DisplayBoard(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]); if (i < rows - 1) { printf("---|---|---\n"); } }}void ComputerMove(char board[ROWS][COLS], int rows, int cols){ int x = 0; int y = 0; printf("computer go\n"); x = rand() % rows; y = rand() % cols; while (1) { if (board[x][y] == ' ') { board[x][y] = 'X'; break; } }}void PlayerMove(char board[ROWS][COLS], int rows, int cols){ int x = 0; int y = 0; printf("player go:"); scanf("%d%d", &x, &y); while (1) { if (x >= 1 && x <= rows && y >= 1 && y <= cols) { if (board[x-1][y-1] == ' ') { board[x-1][y-1] = '*'; break; } else { printf("Can't input here.\n"); break; } } else { printf("input error,please input again\n"); } }}static int is_full(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++) { if (board[i][j] == ' ') return 0; } } return 1;}char CheakWin(char board[ROWS][COLS], int rows, int cols){ int i = 0; for (i = 0; i < rows; i++) { if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } } for (i = 0; i < cols; i++) { if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ') { return board[0][i]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } if (is_full(board, rows, cols)) { return 'q'; } return ' ';}
阅读全文
0 0
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋,
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- 三子棋
- JAVA学习笔记--类加载机制
- 2017/10/26
- BigData数据导入工具sqoop入门
- PDF格式转Excel技巧分享
- PHPCMS V9后台表单向导中让“信息列表"页面直接显示用户留言的信息列表
- 三子棋
- JAVA学习笔记--线程状态及对象锁
- Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar的解决办法
- Android实现滑动效果时选择FragmentStatePagerAdapter还是FragmentPagerAdapter?
- Python OpenCV格式和PIL.Image格式 互转
- S32K144 EVB之RTC
- JAVA学习笔记--GC日志解析
- 30天自制操作系统之第二天Makefile
- 主机ping不通虚拟机