三子棋项目
最近在快乐的学习C语言,目前学习到一个小小的阶段,跟着视频敲项目,于是做了这个三子棋项目,灰常的简单,简单到有手就行。 结局很明显,我用了我超高的智商,赢得了此次的比赛。 (我要打十个!)
开发环境
编辑器: CLion main.c
#include "game.h"
void menu(){
printf("*******************************\n");
printf("******* 1. 开始 *******\n");
printf("******* 0. 退出 *******\n");
printf("*******************************\n");
}
void game(){
char board[ROW][COL];
InitBoard(board,ROW,COL);
DisplayBoard(board,ROW,COL);
char ret = 0;
while (1){
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board,ROW,COL);
if(ret != 'C')
break;
ComputerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board,ROW,COL);
if(ret != 'C')
break;
}
if(ret == '*'){
printf("玩家赢了\n");
}else{
printf("平局\n");
}
DisplayBoard(board,ROW,COL);
}
int main(){
int input;
srand((unsigned int) time(NULL));
do{
menu();
printf("请选择:>");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("Bey Bey! \n");
break;
}
}while (input);
return 0;
}
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL],int row,int col);
void DisplayBoard(char board[ROW][COL],int row,int col);
void PlayerMove(char board[ROW][COL],int row,int col);
void ComputerMove(char board[ROW][COL],int row,int col);
char IsWin(char board[ROW][COL],int row,int col);
game.c
#include "game.h"
void InitBoard(char board[ROW][COL],int row,int col){
int i = 0;
int j = 0;
for(i = 0; i< row;i++){
for(j=0;j<col;j++){
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL],int row,int col){
int i=0;
for(i=0;i<row;i++){
for (int j = 0; j < col; ++j) {
if(j < col-1)
printf(" %c |",board[i][j]);
else
printf(" %c \n",board[i][j]);
}
if(i < row-1)
for (int j = 0; j < row; ++j) {
if(j < row -1)
printf("---|");
else
printf("---\n");
}
}
}
void PlayerMove(char board[][COL],int row,int col){
int x = 0;
int y = 0;
printf("玩家走:>\n");
while (1){
printf("请输入下棋坐标:>");
scanf("%d %d", &x, &y);
if(x >= 1 && x <= row && y >= 1 && y <= col){
if(board[x-1][y-1] == ' '){
board[x-1][y-1] = '*';
break;
}else{
printf("坐标被占用,请重新输入\n");
}
}else{
printf("坐标非法,请重新输入\n");
}
}
}
void ComputerMove(char board[ROW][COL],int row,int col){
printf("电脑走:>\n");
while (1){
int x = rand() % row;
int y = rand() % col;
if(board[x][y] == ' '){
board[x][y] = '#';
break;
}
}
}
int isFull(char board[ROW][COL],int row,int col){
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if(board[i][j] == ' ')
return 0;
}
}
return 1;
}
char IsWin(char board[ROW][COL],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[2][2] != ' ') {
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
return board[1][1];
}
int ret = isFull(board,ROW,COL);
if(ret == 1)
return 'Q';
return 'C';
}
快乐的回顾了一下我写的项目!
|