C語(yǔ)言實(shí)現(xiàn)三子棋游戲附注釋
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)三子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
概述
三子棋棋盤為九宮格形式,玩家和電腦分別輪流落子,若有一方有三個(gè)棋連在一起的情況則勝。
實(shí)現(xiàn)過(guò)程
1、玩家交互菜單創(chuàng)建
2、棋盤創(chuàng)建與初始化
3、玩家與電腦落子
4、判定勝負(fù)關(guān)系
多文件實(shí)現(xiàn)
頭文件 game.h
#ifndef __GAME_H__ #define __GAME_H__ #include <stdio.h> #include <time.h> #include <stdlib.h> #include <windows.h> #define ROW 3 #define COL 3 #define INIT ' ' //宏定義 空 #define WHITE 'X' //Player #define BLACK 'O' //Computer #define DRAW 'D' #define NEXT 'N' #pragma warning(disable:4996) extern void Game(); #endif
源文件 main.c
#include "game.h"
void Menu()
{
printf("+-------------------------------+\n");
printf("| 1. Play 0. Exit |\n");
printf("+-------------------------------+\n");
}
int main()
{
int select = 0;
int quit = 0;
while (!quit)
{
Menu();
printf("Please Select# ");
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 0:
quit = 1;
break;
default:
printf("Enter 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) //對(duì)九宮格棋盤進(jìn)行初始化
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++) //雙重循環(huán) 二維數(shù)組 矩陣
{
board[i][j] = INIT; //INIT宏定義為空
}
}
}
static void ShowBoard(char board[][COL], int row, int col) //函數(shù) 打印棋盤
{
system("cls"); //調(diào)用dos命令窗口進(jìn)行清屏操作,實(shí)現(xiàn)刷新棋盤
printf(" ");
for (int i = 0; i < col; i++)
{
printf("%4d", i+1);
}
printf("\n--------------\n");
for (int i = 0; i < row; i++)
{
printf("%-2d", i+1); //2
for (int j = 0; j < col; j++)
{
printf("| %c ", board[i][j]); //12
}
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] && \ //判斷右對(duì)角線是否三子連線
board[1][1] == board[2][2] && \
board[1][1] != INIT)
{
return board[1][1];
}
if (board[0][2] == board[1][1] && \ //判斷左對(duì)角線是否三子連線
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; //定義<x,y>坐標(biāo)方式實(shí)現(xiàn)玩家落子
while (1)
{
printf("Please Enter Position <x,y># ");
scanf("%d %d", &x, &y);
if (x < 1 || y < 1 || x > 3 || y > 3) //非法判斷
{
printf("Enter Position Error!\n");
continue;
}
if (board[x - 1][y - 1] == INIT) //當(dāng)前位置為空方可落子
{
board[x - 1][y - 1] = WHITE;
break;
}
else
{
printf("Position Is Not Empty!\n");
}
}
}
static void ComputerMove(char board[][COL], int row, int col) //電腦落子
{
while (1){
int x = rand() % row;
int y = rand() % col; //利用隨機(jī)數(shù)種子 實(shí)現(xiàn)電腦落子
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)); //生成隨機(jī)數(shù)種子
char result = 0;
while (1){
ShowBoard(board, ROW, COL); //顯示棋盤
PlayerMove(board, ROW, COL); //玩家落子
result = IsEnd(board, ROW, COL); //判斷游戲狀態(tài)
if (result != NEXT){
break;
}
ShowBoard(board, ROW, COL); //顯示棋盤
ComputerMove(board, ROW, COL); //電腦落子
result = IsEnd(board, ROW, COL); //判斷游戲狀態(tài)
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("You == Computer!\n");
break;
default:
printf("BUG!\n"); //Do Nothing!
break;
}
}
三種結(jié)果展示



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用C語(yǔ)言實(shí)現(xiàn)三子棋(井字棋)小游戲
這篇文章主要為大家詳細(xì)介紹了利用C語(yǔ)言實(shí)現(xiàn)三子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C語(yǔ)言實(shí)現(xiàn)打印星號(hào)圖案
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)打印星號(hào)圖案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
基于C++實(shí)現(xiàn)俄羅斯方塊游戲的示例代碼
俄羅斯方塊(Tetris)是一款風(fēng)靡全球的經(jīng)典益智游戲,自1984年首次發(fā)布以來(lái),便吸引了無(wú)數(shù)玩家,在這篇博文中,我們將深入探討如何用 C++ 編寫一個(gè)簡(jiǎn)單的俄羅斯方塊游戲,我們將從游戲的基本概念和設(shè)計(jì)入手,逐步實(shí)現(xiàn)游戲的各個(gè)功能模塊,感興趣小伙伴快來(lái)看看吧2024-11-11
如何查看進(jìn)程實(shí)際的內(nèi)存占用情況詳解
本篇文章是對(duì)如何查看進(jìn)程實(shí)際的內(nèi)存占用情況進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++?指針和對(duì)象成員訪問(wèn)的區(qū)別:`.`?與?`->`?的使用小結(jié)
在學(xué)習(xí)?C++?時(shí),常常會(huì)遇到訪問(wèn)對(duì)象成員的兩種符號(hào):.?和?->,這兩個(gè)符號(hào)看似簡(jiǎn)單,但它們的正確使用卻需要理解指針和對(duì)象的本質(zhì)差異,本文介紹C++?指針和對(duì)象成員訪問(wèn)的區(qū)別:`.`?與?`->`?的使用指南,感興趣的朋友一起看看吧2024-12-12

