本文主要是介绍小小三子棋,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写呀写呀写终于写了个三子棋
三子棋
头文件
#ifndef _CHESS_H_
#define _CHESS_H_#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>#pragma warning(disable:4996)#define ROW 3
#define COL 3#define BLACK_PIECE 'X'
#define WHITE_PIECE 'O'void Game();
void InitBoard(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
int PlayerMove(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);
char JudgeResult(char board[][COL], int row, int col);#endif
定义规则及判定的test.c
#include "test.h"void InitBoard(char board[][COL], int row, int col)
{int i = 0;for (; i < row; i++){int j = 0;for (; j < col; j++){board[i][j] = ' ';}}
}void ShowBoard(char board[][COL], int row, int col)//显示棋盘
{printf(" \\ | [ 1 ] | [ 2 ] | [ 3 ] |\n");int i = 0;for (; i < row; i++){printf("----------------------------\n");printf(" %d | [ %c ] | [ %c ] | [ %c ] |\n", i + 1, board[i][0], board[i][1], board[i][2]);}printf("----------------------------\n");
}int PlayerMove(char board[][COL], int row, int col)
{int x = 0;int y = 0;printf("Please Enter Your Pos<x, y># ");scanf("%d %d", &x, &y);if (x >= 1 && x <= 3 && y >= 1 && y <= 3){if (board[x - 1][y - 1] != ' '){return 2;}board[x - 1][y - 1] = BLACK_PIECE;return 0;}return 1; //用户输入坐标有误
}//用户赢了 BLACK_PIECE 'X'
//电脑赢了 WHITE_PIECE 'O'
//平局, 'E'
//next, 'N'
char JudgeResult(char board[][COL], int row, int col)
{int i = 0;for (; i < row; i++){if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]){return board[i][0];}}for (i = 0; i < col; i++){if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i]){return board[0][i];}}if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]){return board[0][0];}if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]){return board[1][1];}for (i = 0; i < row; i++){int j = 0;for (; j < col; j++){if (board[i][j] == ' '){return 'N';}}}return 'E';
}void ComputerMove(char board[][COL], int row, int col)
{while (1){int i = rand() % row;int j = rand() % col;if (board[i][j] == ' '){board[i][j] = WHITE_PIECE;break;}}printf("Computer ... Done\n");Sleep(1000);
}
void Game()
{char board[ROW][COL];InitBoard(board, ROW, COL);char result = 'N';srand((unsigned long)time(NULL));while (1){ShowBoard(board, ROW, COL);int type = PlayerMove(board, ROW, COL);if (1 == type){printf("坐标有误,请重输\n");continue;}else if (2 == type){printf("你输入的坐标已经被占用,请重新输入!\n");continue;}else{printf("Player ... Done!\n");}result = JudgeResult(board, ROW, COL);if (result != 'N'){break;}ComputerMove(board, ROW, COL);result = JudgeResult(board, ROW, COL);if (result != 'N'){break;}}ShowBoard(board, ROW, COL);switch (result){case 'E':printf("平局!\n");break;case BLACK_PIECE:printf("u win!\n");break;case WHITE_PIECE:printf("u lost!\n");break;default://bug!!break;}
}
简略来讲如下
- 初始化棋盘
- 显示棋盘
- 人走
- 判定(显示)
- 机走
- 判定(显示)
- 。。。
- 判定输赢(显示)
设立界面的main.c
#include "test.h"void Menu()//建立菜单
{printf("######################\n");printf("###### 三子棋 ######\n");printf("### 1.Play 2.Exit ##\n");printf("######################\n");printf("please select: >> ");
}int main()
{int quit = 0;while (!quit){int select = 0;Menu();scanf("%d", &select);switch (select){case 1:Game();break;case 2:quit = 1;break;default:printf("Enter Error!\n");break;}}system("pause");return 0;
}
包含菜单等
这篇关于小小三子棋的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!