1.頭文件內容:
windows
#pragma once #include <stdio.h> #include <string.h> #include <math.h> #include <windows.h> #include <stdlib.h> #include <time.h> #define ROW 3 #define COL 3 void text(void); void menu(void); void unlook(char ch[ROW][COL], int row, int col); void display(char ch[ROW][COL], int row, int col); void player(char ch[ROW][COL], int row, int col); void computer(char ch[ROW][COL], int row, int col); char Isright(char ch[ROW][COL], int row, int col); //可能會有四種結果 //電腦贏..........# //玩家贏..........* //平局............P(格子滿了都沒有分出勝負) //繼續............Q
2.主文件:
數組
#include "1.h" int main() { text(); return 0 ; }
3.函數存放文件:ide
#include "1.h" void player(char ch[ROW][COL], int row, int col) { int i = 0; int j = 0; printf("遊戲開始:"); while (1) { printf("玩家走--->"); scanf_s("%d %d", &i, &j); if (i >= 1 && j <= col) { if (ch[i - 1][j - 1] == ' ') { ch[i - 1][j - 1] = '*'; break; } else { printf("該位置已經被佔用,請從新輸入.\n"); } } else { printf("格式違法,請從新輸入:--->\n"); } } } void computer(char ch[ROW][COL], int row, int col) { int i = 0; int j = 0; printf("電腦走:-->\n"); while (1) { i = rand() % row; j = rand() % col; if (ch[i][j] == ' ') { ch[i][j] = '#'; break; } //測試 /*else { printf ("該位置已經被佔用\n"); }*/ } } char Isright(char ch[ROW][COL], int row, int col) { int i, j; //行相等 for (i = 0; i < row; i++) { if (ch[i][0] == ch[i][1] && ch[i][1] == ch[i][2] && ch[i][1] != ' ') { return ch[i][0]; } } //列相等 for (i = 0; i < row; i++) { if (ch[0][i] == ch[1][i] && ch[0][i] == ch[2][i] && ch[1][i] != ' ') { return ch[0][i]; } } //隊列相等 if (ch[0][0] == ch[1][1] && ch[1][1] == ch[2][2] && ch[1][1] != ' ') { return ch[0][0]; } if (ch[0][2] == ch[1][1] && ch[1][1] == ch[2][0] && ch[1][1] != ' ') { return ch[0][2]; } //平局 for (i = 0; i < row; i++) { j = 0; for (j = 0; j < col; j++) { if (ch[i][j] == ' ') { return 'Q'; } } } return 'P'; } void menu(void) { printf("***********************************************\n"); printf("* 1.>>>>>進行遊戲 0.>>>>> 退出遊戲 *\n"); printf("***********************************************\n"); } //重置數組函數 void unlook(char ch[ROW][COL], int row, int col) { int i, j; for (i = 0; i < row; i++) { j = 0; for (j = 0; j < col; j++) { ch[i][j] = ' '; } } } //打印數組函數 void display(char ch[ROW][COL], int row, int col) { int i, j; for (i = 0; i < row; i++) { //1.打印每一行 j = 0; for (j = 0; j < col; j++) { printf(" %c ", ch[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); //2.行分割; if (i < row - 1) { j = 0; for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } } printf("\n"); } } void game() { char sc; char ch[ROW][COL] = { 0 }; printf("請稍等即將進入遊戲:"); Sleep(1000); printf("-"); Sleep(1000); printf("-"); Sleep(1000); printf("-"); Sleep(1000); printf("-"); Sleep(1000); printf(">\n"); Sleep(1000); /*重置數組:*/ unlook(ch, ROW, COL); //打印棋盤 /* __ | __ | __ __ | __ | __ __ | __ | __*/ display(ch, ROW, COL); //開始下棋 while (1) { //玩家走 player(ch, ROW, COL); //顯示數組形式 sc = Isright(ch, ROW, COL); if (sc != 'Q') { break; } display(ch, ROW, COL); //電腦走 computer(ch, ROW, COL); /*顯示電腦數組形式*/ sc = Isright(ch, ROW, COL); if (sc != 'Q') { break; } display(ch, ROW, COL); } if (sc == '*') { printf("******玩家WIN******\n\n\n\n"); printf("即將退出遊戲:\n\n\n"); } else { if (sc == '#') { printf("******電腦WIN******\n\n\n\n"); printf("即將退出遊戲:\n\n\n"); } else { printf("******平局******\n\n\n\n"); } } } void text(void) { int num = 0; srand((unsigned int)time(NULL)); do { menu(); printf("請選擇:----->"); scanf_s("%d", &num); switch (num) { case 1: printf("五子棋.\n"); game(); break; case 0: printf("退出程序.\n"); Sleep(1000); system("cls"); break; default: Sleep(1000); printf(">>>>>>>>>>>>沒法識別:%d\n", num); printf("請輸入0或1用以退出或進行遊戲:\n"); } } while (num); }