您好,登錄后才能下訂單哦!
本篇內容介紹了“C語言實現三子棋小游戲的代碼怎么寫”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1.創建一個游戲選擇面板.
2.創建并初始化棋盤。
3.玩家落子并判定,電腦落子并判定。
4.判定結果 ,游戲結束!
頭文件:
#pragma once #include<stdio.h> #include<windows.h> #include<stdlib.h> #include <time.h> #pragma warning(disable:4996) extern void Game();//游戲代碼需要定義調用的函數 #define ROW 3//FOW、FOL代表三子棋邊界的長和寬 #define COL 3 #define INIT ' ' //INIT為空格的宏定義 #define WHITE 'X' //X在三字棋代表Player #define BLACK 'O' //O在三字棋代表Computer #define DRAW 'D' //DRAW代表平局 #define NEXT 'N' //NEXT代表繼續 //MineClearence
各類函數:
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("%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]); } 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[1][1] != INIT){ return board[1][1]; } 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 Postion<x,y># "); scanf("%d %d", &x, &y); if (x < 1 || y < 1 || x > 3 || y > 3){ printf("Enter Postion Error!\n"); 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("You == Computer!\n"); break; default: printf("BUG!\n"); break; } }
游戲選擇面板(主函數)
#include "game.h" static void Menu() { printf("|------------- 開始游戲 -------------|\n"); printf("+------------- 1. Play -------------+\n"); printf("+------------- 0. Exit -------------+\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("bye!\n"); system("pause"); return 0; }
“C語言實現三子棋小游戲的代碼怎么寫”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。