C語言實(shí)現(xiàn)三子棋游戲附注釋
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)三子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
概述
三子棋棋盤為九宮格形式,玩家和電腦分別輪流落子,若有一方有三個(gè)棋連在一起的情況則勝。
實(shí)現(xiàn)過程
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)文章
關(guān)于C++中由于字節(jié)對(duì)齊引起內(nèi)存問題定位分析
前幾天遇到一個(gè)稀奇古怪的問題,在創(chuàng)建對(duì)象的時(shí)候程序異常退出,查找代碼發(fā)現(xiàn)結(jié)構(gòu)體數(shù)組問題,最終把問題簡(jiǎn)化得到解決方法,下面小編把我的問題及解決方案分享到腳本之家平臺(tái)供大家參考下2021-06-06C++ boost 時(shí)間與日期處理詳細(xì)介紹
這篇文章主要介紹了C++ boost 時(shí)間與日期處理詳細(xì)介紹的相關(guān)資料,這里提供實(shí)例代碼,及實(shí)現(xiàn)效果,需要的朋友可以參考下2016-11-11C++?STL中五個(gè)常用算法使用教程及實(shí)例講解
本文主要介紹了C++?STL算法中常見的五個(gè)算法的使用教程并附上了案例詳解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11C++實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)最新版
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)最新版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C++語言實(shí)現(xiàn)hash表詳解及實(shí)例代碼
這篇文章主要介紹了C++語言實(shí)現(xiàn)hash表詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01C++實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01