用C語言實現(xiàn)簡單的三子棋
三子棋代碼的實現(xiàn)需要一個簡單的思路做指引,所以我們先來做一下思路的整理,代碼的實現(xiàn)主要分為以下幾個步驟:
1.初始化數(shù)組
2.顯示數(shù)組
3.電腦走
4.玩家走
5.判斷輸贏
所以,先寫出源文件game.h,如下。
#ifndef __GAME_H__ ? #define __GAME_H__ ? #define ROWS 3 ? //定義行 ? #define COLS 3 ? //定義列 ? //初始化數(shù)組 void init_board(char board[ROWS][COLS], int rows, int cols); ?//顯示數(shù)組 void display_board(char board[ROWS][COLS], int rows, int cols); //電腦走 void computer_move(char board[ROWS][COLS], int rows, int cols); //玩家走 void player_move(char board[ROWS][COLS], int rows, int cols); //判斷輸贏 char check_win(char board[ROWS][COLS], int rows, int cols); #endif//__GAME_H__
接下來再寫出主體的main函數(shù),使這個三子棋的大體先做出來。以下為main.c函數(shù)。要實現(xiàn)的主要為選擇界面以及大體的順序。
#define _CRT_SECURE_NO_WARNINGS ?? #include"game.h" ? #include<stdio.h> ? #include<stdlib.h> ? #include<time.h> ? void menu() { ? ? printf("*******************************\n"); ? ? printf("*****1.paly ? ?2.exit *********\n"); ? ? printf("*******************************\n"); } void game() { ? ? char ret = 0; ? ? char board[ROWS][COLS] = { 0 }; ? ? init_board(board, ROWS, COLS); ? ? while (1) ? ? { ? ? ? ? printf("電腦走:\n"); ? ? ? ? computer_move(board, ROWS, COLS); ? ? ? ? display_board(board, ROWS, COLS); ? ? ? ? ret = check_win(board, ROWS, COLS); ? ? ? ? if (ret != 'q') ? ? ? ? { ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? printf("玩家走:\n"); ? ? ? ? player_move(board, ROWS, COLS); ? ? ? ? display_board(board, ROWS, COLS); ? ? ? ? ret = check_win(board, ROWS, COLS); ? ? ? ? if (ret != 'q') ? ? ? ? { ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? if (ret == '*') ? ? { ? ? ? ? printf("玩家贏了!\n"); ? ? } ? ? else if (ret == 'o') ? ? { ? ? ? ? printf("電腦贏了!\n"); ? ? } ? ? else if (ret == ' ') ? ? { ? ? ? ? printf("平局!\n"); ? ? } } int main() { ? ? int input = 0; ? ? srand((unsigned)time(NULL)); ? ? do ? ? { ? ? ? ? menu(); ? ? ? ? printf("請選擇:"); ? ? ? ? scanf("%d", &input); ? ? ? ? switch (input) ? ? ? ? { ? ? ? ? case 1: ? ? ? ? ? ? game(); ? ? ? ? ? ? break; ? ? ? ? case 2: ? ? ? ? ? ? return 0; ? ? ? ? ? ? break; ? ? ? ? default: ? ? ? ? ? ? printf("輸入錯誤,請重新輸入!\n"); ? ? ? ? ? ? break; ? ? ? ? } ? ? } while (input); ? ? system("pause"); ? ? return 0; }
以下為函數(shù)模塊功能的的分布實現(xiàn):
#define _CRT_SECURE_NO_WARNINGS ?? #include"game.h" ? #include<stdio.h> ? #include<stdlib.h> ? #include<time.h> ? void init_board(char board[ROWS][COLS], int rows, int cols) { ? ? int i = 0; ? ? int j = 0; ? ? for (i = 0; i < rows; i++) ? ? { ? ? ? ? for (j = 0; j < cols; j++) ? ? ? ? { ? ? ? ? ? ? board[i][j] = ' '; ? ? ? ? } ? ? } } void display_board(char board[ROWS][COLS], int rows, int cols) ? ? ? ? ? ? ? ? ? ?//顯示棋盤 ? { ? ? int i = 0; ? ? for (i = 0; i < rows; i++) ? ? { ? ? ? ? printf("| ?%c | ?%c | ?%c |\n", board[i][0], board[i][1], board[i][2]); ? ? ? ? if (i <= rows - 1) ? ? ? ? { ? ? ? ? ? ? printf("|----|----|----|\n"); ? ? ? ? } ? ? } } void computer_move(char board[ROWS][COLS], int rows, int cols) ? ? ? ? ? //電腦走 ? { ? ? while (1) ? ? { ? ? ? ? int x = rand() % 3; ? ? ? ? int y = rand() % 3; ? ? ? ? if (board[x][y] != '*' && board[x][y] != 'o') ? ? ? ? { ? ? ? ? ? ? board[x][y] = 'o'; ? ? ? ? ? ? return; ? ? ? ? } ? ? } } void player_move(char board[ROWS][COLS], int rows, int cols) ? ? ? ? ?//玩家走 ? { ? ? int x = 0; ? ? int y = 0; ? ? printf("玩家請輸入位置,如:x y》"); ? ? do ? ? { ? ? ? ? scanf("%d %d", &x, &y); ? ? ? ? if (board[x - 1][y - 1] == 'o') ? ? ? ? { ? ? ? ? ? ? printf("該位置已經(jīng)被占用,請重新輸入:》"); ? ? ? ? } ? ? ? ? else if (((x - 1)>3) || ((y - 1)>3) || (x - 1 < 0) || (y - 1 < 0)) ? ? ? ? { ? ? ? ? ? ? printf("輸入位置錯誤,請重新輸入:》"); ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? board[x - 1][y - 1] = '*'; ? ? ? ? ? ? break; ? ? ? ? } ? ? } while (1); } static int is_full(char board[ROWS][COLS], int rows, int cols) ? ?//判斷棋盤滿沒有 ? { ? ? int i = 0; ? ? int j = 0; ? ? for (i = 0; i < rows; i++) ? ? { ? ? ? ? for (j = 0; j <cols; j++) ? ? ? ? { ? ? ? ? ? ? if (board[i][j] == ' ') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return 0; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? return 1; } char check_win(char board[ROWS][COLS], int rows, int cols) ? ? ? ? ? //判斷輸贏 ? { ? ? int k = 1; ? ? int i = 0; ? ? for (i = 0; i < rows; i++) ?//判斷行 ? ? { ? ? ? ? if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && board[i][0] != ' ')? ? ? ? ? { ? ? ? ? ? ? return board[i][0]; ? ? ? ? } ? ? } ? ? for (i = 0; i < cols; i++) ?//判斷列 ? ? { ? ? ? ? if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] != ' ')) ? ? ? ? { ? ? ? ? ? ? return board[0][i]; ? ? ? ? } ? ? } ? ? if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[0][0] != ' ')) //判斷正斜 ? ? { ? ? ? ? return board[1][1]; ? ? } ? ? if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[0][2] != ' ')) //判斷反斜 ? ? { ? ? ? ? return board[0][2]; ? ? } ? ? if (is_full(board, rows, cols)) ? ? { ? ? ? ? return ' '; ? ? } ? ? return 'q'; }
以下為程序運行的三種結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Qt?關(guān)于容器的遍歷迭代器的使用問題小結(jié)
Qt是一個跨平臺的 C++ 開發(fā)庫,主要用來開發(fā)圖形用戶界面程序,當然也可以開發(fā)不帶界面的命令行程序,本文重點給大家介紹Qt?關(guān)于容器的遍歷迭代器的使用問題小結(jié),感興趣的朋友一起看看吧2022-03-03c++實現(xiàn)對輸入數(shù)組進行快速排序的示例(推薦)
下面小編就為大家?guī)硪黄猚++實現(xiàn)對輸入數(shù)組進行快速排序的示例(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06VC++實現(xiàn)的OpenGL線性漸變色繪制操作示例
這篇文章主要介紹了VC++實現(xiàn)的OpenGL線性漸變色繪制操作,結(jié)合實例形式分析了VC++基于OpenGL進行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07