用C語(yǔ)言實(shí)現(xiàn)三子棋
本文實(shí)例為大家分享了用C語(yǔ)言實(shí)現(xiàn)三子棋的具體代碼,供大家參考,具體內(nèi)容如下
三子棋含義:
三子棋是黑白棋的一種。三子棋又叫九宮棋、圈圈叉叉、一條龍、井字棋等。將正方形對(duì)角線(xiàn)連起來(lái),相對(duì)兩邊依次擺上三個(gè)雙方棋子,只要將自己的三個(gè)棋子走成一條線(xiàn),對(duì)方就算輸了。但是,有很多時(shí)候會(huì)出現(xiàn)和棋的情況。圖例如下:

基本思路:
1.創(chuàng)建用戶(hù)交互菜單界面
2.初始化棋盤(pán)
3.顯示棋盤(pán)面板(為了不重復(fù)顯示棋盤(pán),使用清屏操作)
4.用戶(hù)落子
5.判斷勝負(fù)
6.電腦隨機(jī)正確落子
7.判斷勝負(fù)
(在每次寫(xiě)程序之前,可以向如下圖所示,寫(xiě)出思路或偽代碼)

創(chuàng)建多文件項(xiàng)目:
寫(xiě)代碼之前,首先建立三個(gè)文件,以方便后序代碼更加完整清晰地呈現(xiàn)。

1.創(chuàng)建用戶(hù)交互菜單界面
void Meau(){
printf("+------ meau ----------+\n");
printf("|---- 1.play --------|\n");
printf("|---- 0.quit --------|\n");
printf("+----------------------+\n");
}
2.初始化棋盤(pán)
使用宏定義,將棋盤(pán)中的內(nèi)容初始化為空。
static void InitBoard(char board[][COL], int row, int col){
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
board[i][j] = INIT;
}
}
}
3.顯示棋盤(pán)面板
為了不重復(fù)顯示棋盤(pán),使用清屏操作,使得每次現(xiàn)實(shí)的棋盤(pán)只有一張。
通過(guò)不斷調(diào)試,使得最終界面,與預(yù)期所需界面一致。
static void ShowBoard(char board[][COL],int row,int col){
system("cls");//清屏操作
printf(" ");
for (int i = 0; i < col; i++){
printf(" %3d", i + 1);
}
printf("\n----------------\n");
for (int i = 0; i < row; i++){
printf("%-2d", i + 1);
for (int j = 0; j < col; j++){
printf("| %c ", board[i][j]);
}
printf("\n----------------\n");
}
}
4.用戶(hù)落子
落子只能落在為空的位置上,所以在落子前需要判空,若為空,則落子,否則提示重新落子。
static void PlayerMove(char board[][COL], int row, int col){
int x = 0;
int y = 0;
while (1){
printf("please enter your postion<x,y>: ");
scanf("%d %d", &x, &y);
if (x<1 || x>3 || y<1 || y>3){
printf("Postion Error!");
continue;
}
if (board[x - 1][y - 1] == INIT){
board[x - 1][y - 1] = WHITE;
break;
}
else{
printf("Postion Is Not Empty!\n");
}
}
}
5.電腦隨機(jī)正確落子
使用隨機(jī)數(shù),在正確位置落子。
static void ComputerMove(char board[][COL], int row, int col){
while (1){
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == INIT){
board[x][y] = BLACK;
break;
}
}
}
6.判斷勝負(fù)
static char IsEnd(char board[][COL], int row, int col){
for (int i = 0; i < row; i++){
if (board[i][0] == board[i][1] && \
board[i][1] == board[i][2] && \
board[i][0] != INIT){
return board[i][0];
}
}
for (int j = 0; j < col; j++){
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[0][j] != INIT){
return board[0][j];
}
}
if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[0][0] != INIT){
return board[0][0];
}
if(board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != INIT){
return board[1][1];
}
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
if (board[i][j] == INIT);
return NEXT;
}
}
return DRAW;
}
7.創(chuàng)建Game界面
void Game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
srand((unsigned long)time(NULL));
char result = 0;
while (1){
ShowBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
break;
}
ShowBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
break;
}
}
ShowBoard(board, ROW, COL);
switch (result){
case WHITE:
printf("You win!\n");
break;
case BLACK:
printf("you lose!\n");
break;
case DRAW:
printf("it ends in a draw\n");
break;
defult:
printf("bug\n");
break;
}
}
完整代碼
//main.c文件
#include"game.h"
void Meau(){
printf("+------ meau ----------+\n");
printf("|---- 1.play --------|\n");
printf("|---- 0.quit --------|\n");
printf("+----------------------+\n");
}
int main(){
int select = 0;
int quit = 0;
while (!quit)
{
Meau();
printf("please enter your choose: ");
scanf("%d", &select);
switch (select)
{
case 1:
Game();
break;
case 0:
quit = 1;
break;
defult:
printf("Select error!Try again!\n");
break;
}
}
printf("byebye!\n");
system("pause");
return 0;
}
//game.c 文件
#include"game.h"
static void InitBoard(char board[][COL], int row, int col){
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
board[i][j] = INIT;
}
}
}
static void ShowBoard(char board[][COL],int row,int col){
system("cls");
printf(" ");
for (int i = 0; i < col; i++){
printf(" %3d", i + 1);
}
printf("\n----------------\n");
for (int i = 0; i < row; i++){
printf("%-2d", i + 1);
for (int j = 0; j < col; j++){
printf("| %c ", board[i][j]);
}
printf("\n----------------\n");
}
}
static char IsEnd(char board[][COL], int row, int col){
for (int i = 0; i < row; i++){
if (board[i][0] == board[i][1] && \
board[i][1] == board[i][2] && \
board[i][0] != INIT){
return board[i][0];
}
}
for (int j = 0; j < col; j++){
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[0][j] != INIT){
return board[0][j];
}
}
if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[0][0] != INIT){
return board[0][0];
}
if(board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != INIT){
return board[1][1];
}
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
if (board[i][j] == INIT);
return NEXT;
}
}
return DRAW;
}
static void PlayerMove(char board[][COL], int row, int col){
int x = 0;
int y = 0;
while (1){
printf("please enter your postion<x,y>: ");
scanf("%d %d", &x, &y);
if (x<1 || x>3 || y<1 || y>3){
printf("Postion Error!");
continue;
}
if (board[x - 1][y - 1] == INIT){
board[x - 1][y - 1] = WHITE;
break;
}
else{
printf("Postion Is Not Empty!\n");
}
}
}
static void ComputerMove(char board[][COL], int row, int col){
while (1){
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == INIT){
board[x][y] = BLACK;
break;
}
}
}
void Game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
srand((unsigned long)time(NULL));
char result = 0;
while (1){
ShowBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
break;
}
ShowBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
break;
}
}
ShowBoard(board, ROW, COL);
switch (result){
case WHITE:
printf("You win!\n");
break;
case BLACK:
printf("you lose!\n");
break;
case DRAW:
printf("it ends in a draw\n");
break;
defult:
printf("bug\n");
break;
}
}
//game.h文件
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#include<windows.h>
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
#define INIT ' '
#define WHITE 'X'//player
#define BLACK 'O'//computer
#define DRAW 'D'
#define NEXT 'N'
extern void Game();
#endif
代碼結(jié)果顯示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++?list常用接口和模擬實(shí)現(xiàn)實(shí)例代碼
C++中l(wèi)ist容器底層實(shí)現(xiàn)是使用帶頭雙向循環(huán)鏈表的結(jié)構(gòu),通過(guò)指針指向前一個(gè)和后一個(gè)節(jié)點(diǎn),它也具有雙向鏈表的優(yōu)缺點(diǎn),下面給大家介紹C++?list常用接口和模擬實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2025-04-04
C語(yǔ)言實(shí)現(xiàn)整數(shù)逆序的情況解析
今天通過(guò)本文給大家介紹C語(yǔ)言實(shí)現(xiàn)整數(shù)逆序的情況,本文通過(guò)實(shí)例代碼多種舉例給大家介紹的非常詳細(xì),對(duì)C語(yǔ)言整數(shù)逆序相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2021-11-11
C++實(shí)現(xiàn)LeetCode(85.最大矩形)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(85.最大矩形),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言文件操作函數(shù)freopen詳細(xì)解析
替換一個(gè)流,或者說(shuō)重新分配文件指針,實(shí)現(xiàn)重定向。如果stream流已經(jīng)打開(kāi),則先關(guān)閉該流。如果該流已經(jīng)定向,則freopen將會(huì)清除該定向。此函數(shù)一般用于將一個(gè)指定的文件打開(kāi)一個(gè)預(yù)定義的流:標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出或者標(biāo)準(zhǔn)出錯(cuò)2013-10-10

