在學(xué)習(xí)了,一段時(shí)間的C語(yǔ)言后,這次程序可能更讓我看到了一個(gè)學(xué)習(xí)的結(jié)果
//three_chess

成都創(chuàng)新互聯(lián)公司成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元彭澤做網(wǎng)站,已為上家服務(wù),為彭澤各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
//three_chess.h//創(chuàng)立頭文件,用來(lái)存儲(chǔ)函數(shù)聲聲明
#ifndef _THREE_CHESS_H_
#define _THREE_CHESS_H_
#include <stdio.h>
#include <windows.h>
#include <time.h>
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
void ShowUI();
void Game();
void ComputerMove(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);
#endif
//three_chess.c//用來(lái)實(shí)現(xiàn)函數(shù)功能的源代碼
#define _CRT_SECURE_NO_WARNINGS 1
#include "three_chess.h"
void ShowUI()
{
printf("##################################\n");
printf("## 1. Play 2. Exit ##\n");
printf("##################################\n");
printf("Please Select:> ");
}
void ComputerMove(char board[][COL], int row, int col)
{
while (1){
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' '){
board[x][y] = '0';
break;
}
}
}
void PlayerMove(char board[][COL], int row, int col)
{
int x, y;
while (1){
printf("Please Enter Your Pos(x,y):> ");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col){
if (board[x - 1][y - 1] == ' '){
board[x - 1][y - 1] = 'X';
break;
}
else{
printf("Enter Pos Is Not OK, Try Again!\n");
}
}
else{
printf("Enter Error, Try Again!\n");
}
}
}
char Judge(char board[][COL], int row, int col)
{
int i = 0;
int j = 0;
for (; i < row; 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 < col; 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[1][1] != ' '){
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && \
board[1][1] != ' '){
return board[1][1];
}
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
if (board[i][j] == ' '){
return 'N';
}
}
}
return 'E';
}
void ShowBoard(char board[][COL], int row, int col)
{
printf(" 1 2 3\n");
printf("----------------\n");
int i = 0;
int j = 0;
for (; i < row; i++){
printf("%d |", i+1);
for (j=0; j < col; j++){
printf(" %c |", board[i][j]);
}
printf("\n----------------\n");
}
printf("%\n");
}
void Game()
{
char board[ROW][COL];
memset(board, ' ', sizeof(board));
char result = 'N';
srand((unsigned long)time(NULL));
while (1){
system("cls");
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){//'X' 'O' 'E' 'N'
break;
}
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){//'X' 'O' 'E' 'N'
break;
}
}
switch (result){
case 'X':
printf("You Win! :)\n");
break;
case 'O':
printf("You Lose, Computer Win!\n :(");
break;
case 'E':
printf("平局,恭喜!\n");
break;
default:
break;
}
}
//main.c//主函數(shù)用來(lái)通過(guò)調(diào)用函數(shù)來(lái)實(shí)現(xiàn)使用功能
#define _CRT_SECURE_NO_WARNINGS 1
#include "three_chess.h"
int main()
{
int select = 0;
int quit = 0;
while (!quit){
ShowUI();
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
quit = 1;
printf("Bye,Bye!\n");
break;
default:
printf("Please Enter Again!\n");
break;
}
}
system("pause");
return 0;
}
網(wǎng)站題目:C語(yǔ)言實(shí)現(xiàn)三子棋
網(wǎng)站URL:http://www.chinadenli.net/article18/gccsgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、小程序開(kāi)發(fā)、網(wǎng)站制作、微信小程序、網(wǎng)站策劃、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)