C语言编写三子棋
//
// main.c
// helloworld2
//
// Created by xx on 2023/2/9.
// 三子棋
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
//将数组都设置为 ” “
void initBoard(char borad[ROW][COL],int row,int col){
for (int i = 0; i < row; i++) {
for (int j=0; j < col; j++) {
borad[i][j] = ' ';
}
}
}
//将数组有规律的打印
void printBorad(char borad[ROW][COL],int row,int col){
for (int i = 0; i < row; i++) {
for (int j=0; j< col; j++) {
//打印一行数据
printf(" %c ",borad[i][j]);
if(j < col - 1){
printf("|");
}
}
printf("\n");
if(i < row - 1){
for (int j=0; j< col; j++) {
//打印一行数据
printf("---");
if(j < col - 1){
printf("|");
}
}
printf("\n");
}
}
}
//玩家走 ,玩家输入【1 1】
void playerMove(char borad[ROW][COL],int row,int col){
int x = 0,y = 0;
printf("玩家走 > \n");
while (1) {
printf("请输入要下的坐标 > ");
scanf("%d%d",&x,&y);
//对输入的坐标进行判断
if(x >= 1 && x<= row && y >= 1 && y <= col){
//判断是否被占用
if(borad[x-1][y-1] == ' '){
borad[x-1][y-1] = '*';
break;
}else{
printf("该坐标被占用\n");
}
}else{
printf("坐标非法,请重新输入!\n");
}
}
}
//电脑走 ,电脑 随机输入
void computerMove(char borad[ROW][COL],int row,int col){
int x = 0,y = 0;
printf("电脑走 > \n");
while (1) {
x = rand() % row;
y = rand() % col;
if(borad[x][y] == ' '){
borad[x][y] = '#';
break;
}
}
}
//是否是平局 1 - 平局
int isFull(char borad[ROW][COL],int row,int col){
for (int i = 0; i < row; i++) {
for (int j=0; j<col; j++) {
//二维数组还没有铺满
if(borad[i][j] == ' '){
return 0;
}
}
}
return 1;
}
//规则很简单,就是行或列或对角线三个字符一样就赢了
//玩家赢 - ’*‘
//电脑赢 - ’#‘
//平局 - ’Q‘
//继续 - ’C‘
int isWin(char borad[ROW][COL],int row,int col){
//玩家赢
for (int i = 0; i < row; i++) {
//每一行
//不等于’ ‘ 才是关键
if( borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2] && borad[i][0] != ' '){
return borad[i][0];//其实一开始想到返回 *,我觉得写固定不好
}
}
for (int i = 0; i < col; i++) {
//每一列
//不等于’ ‘ 才是关键
if( borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i] && borad[0][i] != ' '){
return borad[0][i];//其实一开始想到返回 *,我觉得写固定不好
}
}
//对角线
if(borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[1][1] != ' '){
return borad[1][1];
}
if(borad[0][2] == borad[1][1] && borad[1][1] == borad[0][2] && borad[1][1] != ' '){
return borad[1][1];
}
//判断是否是平局
if(1 == isFull(borad,ROW,COL)){
return 'Q';
}
return 'C';
}
void game(){
int ret = 0;
//数组存储棋盘信息
char borad[ROW][COL] = {0};
//初始化棋盘
initBoard(borad,ROW,COL);
//打印棋盘
printBorad(borad,ROW,COL);
//下棋
while (1) {
//玩家下
playerMove(borad,ROW,COL);
printBorad(borad, ROW, COL);
//判断玩家赢
ret = isWin(borad, ROW, COL);
if(ret != 'C'){//不是继续的话,直接跳出循环,判断玩家赢、电脑赢、平局
break;
}
//电脑线
computerMove(borad,ROW,COL);
printBorad(borad, ROW, COL);
//判断电脑赢
ret = isWin(borad, ROW, COL);
if(ret != 'C'){
break;
}
}
if(ret == '*'){
printf("玩家赢了!\n");
}else if(ret == '#'){
printf("电脑赢了!\n");
}else{
printf("平局了!\n");
}
}
void menu(){
printf("********************************\n");
printf("***** 1.play 0.exit *********\n");
printf("********************************\n");
}
void test(){
int input = 0;
//生成随机数
srand((unsigned)time(NULL));
do{
menu();
printf("请选择:>");
scanf("%d",&input);
switch (input) {
case 1:
printf("三子棋\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
}while(input);
}
int main(int argc, const char * argv[]) {
test();
return 0;
}