三子棋小游戏
来源:互联网 发布:淘宝隐藏导航条 编辑:程序博客网 时间:2024/06/05 20:14
三子棋小游戏 之前没怎么写过功能复杂一点的程序,在程序的结构,函数的声明引用方面遇到了一些问题。还是得多练。
game.h文件
#ifndef __GAME_H__#define __GAME_H__#include<stdio.h>#include<stdlib.h>#include<string.h>#define ROWS 3#define COLS 3void 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_H__
game.c文件
#define _CRT_SECURE_NO_DEPRECATE 1#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;for(i=0;i<row;i++){printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);if(i<row-1)printf("---|---|---\n");}}void player_move(char board[ROWS][COLS], int row, int col)//玩家输入{ int x,y;while(1) { printf("请输入坐标"); scanf("%d%d",&x,&y); if(x>=1&&x<=row&&y>=1&&y<=col) { x--; y--; 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; } }}int check_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]) //不能使用三连等a==b==c!=d &&(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(check_full(board, row, col)) { return 'D'; //draw} return ' ';}主函数文件test.c
#define _CRT_SECURE_NO_DEPRECATE 1#include<stdio.h>#include<stdlib.h>#include<time.h>#include"game.h"int menu(){printf("*******************************\n");printf("*******1 PIAY 0 EXIT *********\n");printf("*******************************\n");printf(" 请选择 \n");return 0;}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"); display_board(board, ROWS, COLS); } else if(ret == '0') { printf("电脑赢\n"); display_board(board, ROWS, COLS); } else if(ret == 'D') { printf("平局\n"); display_board(board, ROWS, COLS); } }int main(){ int c; do{ menu(); scanf("%d",&c); switch(c){ case 1:game() ;break; case 0:printf("退出游戏\n") ;break; default:printf("选择有误,请重新输入\n") ;break; } }while(c!=0); system("pause"); return 0;}
0 0
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 【小游戏】三子棋
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 三子棋小游戏
- 小游戏:三子棋
- 三子棋小游戏
- 三子棋小游戏
- 三子棋 小游戏
- C语言小游戏"三子棋"
- 超级小游戏:三子棋
- 三子棋小游戏的实现
- Java程序员们值得一看的好书推荐
- JAVA-3.2-认识面向对象
- 设计模式:命令模式
- javascript基本类型和引用类型浅析
- 线性回归和逻辑回归的损失函数
- 三子棋小游戏
- Truncate Table 与DELETE FROM
- shell变量
- Bootstrap笔记
- 欢迎使用CSDN-markdown编辑器
- Ubuntu server v12 Firebird 2.1 创建数据库
- Git 配置及代码下载
- [jzoj]1004. 【东莞市选2007】格斗俱乐部(基础区间DP的各种思路)
- c++实验3——多分数段函数求值