C언어[6]를 처음 배우다

7083 단어 0.0개시하다
game.c//               

#define COL 3  // 
#define ROW 3  // 

#include
#include
#include

void CopyBoard(char board[ROW][COL], int row, int col);
void LOLBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void Player2Move(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);

char IsWin(char board[ROW][COL], int row, int col);
test.c//    ,          
#include"game.h"
void menu()
{
 printf("
"); printf("
"); printf("
"); printf("
"); printf("
"); printf("
"); printf("
"); printf("
"); printf("
"); printf("
"); printf(" *********************************
"); printf("
"); printf(" *********** 1 . play ************
"); printf("
"); printf(" *********** 2 . pvp ************
"); printf("
"); printf(" *********** 0 . exit ************
"); printf("
"); printf(" *********************************
"); } void game() { char ret = 0; // - char board[ROW][COL] = { 0 }; // CopyBoard(board,ROW,COL); // , LOLBoard(board,ROW, COL); // while (1) { // PlayerMove(board, ROW, COL); //LOLBoard(board, ROW, COL); // ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } // ComputerMove(board, ROW, COL); LOLBoard(board, ROW, COL); // ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf(" Player Win
"); } else if (ret == '#') { printf(" Computer Win
"); } else { printf(" same
"); } } void PK() { char ret = 0; char board[ROW][COL] = { 0 }; CopyBoard(board, ROW, COL); LOLBoard(board, ROW, COL); while (1) { PlayerMove(board, ROW, COL); LOLBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } Player2Move(board, ROW, COL); LOLBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("Player Win
"); } else if (ret == '#') { printf("Player2 Win
"); } else { printf("same
"); } } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf(" Please choose!
"); printf(" "); scanf("%d", &input); switch (input) { case 1:game(); break; case 2:PK(); break; case 0:printf(" Quit the game.
"); break; default:printf(" Input error, please select again!
"); break; } } while (input); } int main() { system("color 73"); test(); return 0; }
game.c
#include"game.h"

void CopyBoard(char board[ROW][COL], int row, int col)
{
 int i=0, j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < col; j++)
  {
   board[i][j] = ' ';
  }
 }
}

void LOLBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  printf("                                            ");
  for (j = 0; j < col; j++)
  {
   printf(" %c ", board[i][j]);
   if (j < col - 1)
    printf("|");
  }
  printf("
"); printf(" "); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("
"); } } } // | | //---|---|--- // | | // //---|---|--- // | | void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("player go:->
"); while (1) { printf(" Please input your x and y:->"); scanf("%d%d", &x, &y); // ,x,y if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf(" x and y error,choose
"); } } else { printf(" error,choose again
"); } } } void Player2Move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("player2 go:->
"); while (1) { printf(" Please input your x and 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] = '#'; break; } else { printf(" x and y error,choose
"); } } else { printf(" error,choose again
"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf(" Computer:->
"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } // 1 // 0, // // - '*' // - '#'/ 2 // - 'Q' // - 'C' char IsWin(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ') { return board[i][1]; } } // for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i]; } } // if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') return board[1][1]; if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') return board[1][1]; // if (1 == OrFull(board, ROW, COL)) { return 'Q'; } return 'C'; }

좋은 웹페이지 즐겨찾기