您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)C語(yǔ)言中怎樣實(shí)現(xiàn)三子棋游戲,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
三子棋棋盤為九宮格形式,玩家和電腦分別輪流落子,若有一方有三個(gè)棋連在一起的情況則勝。
1、玩家交互菜單創(chuàng)建
2、棋盤創(chuàng)建與初始化
3、玩家與電腦落子
4、判定勝負(fù)關(guā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é)果展示
關(guān)于“C語(yǔ)言中怎樣實(shí)現(xiàn)三子棋游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。