//測試部分。test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{printf("*************************\n");
printf("******* 1. play *****\n");
printf("******* 0. exit *****\n");
printf("*************************\n");
}
void game()
{ char board[ROW][COL];
char ret = 0;
init_board(board, ROW, COL);
print_board(board, ROW, COL);
while(1)
{player_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{ break;
}
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{ break;
}
}
if (ret == '*')
printf("玩家贏了。\n");
else if (ret == '#')
printf("電腦贏了。\n");
else if (ret == 'Q')
{printf("平局。\n");
}
}
void test()
{srand((unsigned int)time(NULL));
int input = 0;
do
{menu();
printf("請輸入:>(0/1)");
scanf("%d", &input);
switch (input)
{case 1:
game();
break;
case 0:
printf("游戲結(jié)束。\n");
break;
default:
printf("輸入錯誤,請重新輸入。\n");
}
} while (input);
}
int main()
{test();
return 0;
}
//game.h
#pragma once
#include#include#include#define ROW 3
#define COL 3
void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
//game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char board[ROW][COL], int row, int col)
{int i = 0;
for (i = 0; i< row; i++)
{int j = 0;
for (j = 0; j< col; j++)
{ board[i][j] = ' ';
}
}
}
void print_board(char board[ROW][COL], int row, int col)
{int i = 0;
for (i = 0; i< row; i++)
{ int j = 0;
for (j = 0; j< col; j++)
{ printf(" %c ",board[i][j]);
if (j< col - 1)
printf("|");
}
printf("\n");
if (i< row - 1)
{ for (j = 0; j< col; j++)
{printf("---");
if (j< col - 1)
printf("|");
}
printf("\n");
}
}
}
void player_move(char board[ROW][COL], int row, int col)
{printf("玩家下棋。\n");
while (1)
{printf("請輸入要下棋的坐標(biāo):>");
int x = 0;
int y = 0;
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("該坐標(biāo)已被占用,請重新輸入。\n");
}
}
else
{ printf("該坐標(biāo)非法。\n");
}
}
}
void computer_move(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;
}
}
}
static int is_full(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++)
{ if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char is_win(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][0] != ' ')
{ return board[i][0];
}
}
for (i = 0; i< col; i++)
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{return board[i][0];
}
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 (is_full(board, row, col) == 1)
{return 'Q';
}
return 'C';
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
目前成都創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、咸豐網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
文章標(biāo)題:利用C語言實現(xiàn)三子棋-創(chuàng)新互聯(lián)
鏈接地址:http://jinyejixie.com/article48/csdihp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、做網(wǎng)站、品牌網(wǎng)站設(shè)計、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容