From 132ab5ba14adb3bd41b0701e6bdfbc7d92c00d42 Mon Sep 17 00:00:00 2001 From: Lewis Watson Date: Sat, 24 Apr 2021 05:26:53 +0100 Subject: [PATCH] Added Play Against CPU --- gamedata.txt | 3 ++- games.c | 24 ++++++++++++++++++++---- games.h | 6 +++--- main.c | 35 ++++++++++++++++++----------------- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/gamedata.txt b/gamedata.txt index cc416cd..eb1c8ba 100644 --- a/gamedata.txt +++ b/gamedata.txt @@ -4,4 +4,5 @@ NegDiagonalWin;0;4;4;Christos;Lewis;4,4,4,3,3,2,2,4,2,1,1,1,1 PosDiagWin;0;7;6;Lewis;Meg;1,2,2,3,3,4,3,4,5,4,4 MiniTest;0;4;4;P1;P2;1,3,2,3,1,3,2,3 MassiveTest;0;10;10;P1;P2;2,6,3,7,4,10,5 -Custom3x2Board;0;4;2;Lewis;Colin;1,1,2,2,3,3,4 \ No newline at end of file +Custom3x2Board;0;4;2;Lewis;Colin;1,1,2,2,3,3,4 +gameCPU;1;7;6;Lewis;CPU;3,7,2,2,4,7,5 \ No newline at end of file diff --git a/games.c b/games.c index ffabcae..4a58945 100644 --- a/games.c +++ b/games.c @@ -6,7 +6,7 @@ #include "games.h" // Creates game with attributes passed in -void newGame(char *player1, char *player2, int columns, int rows){ +void newGame(int gameType, char *player1, char *player2, int columns, int rows){ //Create Game default Struct Game game = { @@ -17,7 +17,7 @@ void newGame(char *player1, char *player2, int columns, int rows){ .board = constructLinkedMatrix(rows, columns), .pTurn = 1, .log = newEntry(NULL, 0, 0), - .gameType = 0, + .gameType = gameType, .step = 0 }; @@ -45,7 +45,11 @@ int moveController(Game* game){ while (!checkWinConditions(game)) { //Set CurrentPlayer Variable Dependant on playerTurn Game Attribute - if (game->pTurn == 1) {currentPlayer = game->name1;} else {currentPlayer = game->name2;} + if (game->pTurn == 1) {currentPlayer = game->name1;} else { + if(game->gameType!=1){ + currentPlayer = game->name2; + } else {insertCPU(game);currentPlayer = game->name1;} + } // Heading for Game Board Display printf("\n-----Game Board's Current State-----\n\n"); @@ -85,7 +89,7 @@ int moveController(Game* game){ // Next Steps Menu int winMenu = 101; while(!isInRange(1,3,winMenu)||(winMenu!=101)){ - printf("\nMenu Options: \n1. Save Game and Quit to Menu\n2. Quit to Menu (No Save)\n3. Review Game (Enter Analysis Mode)\n\nChoice:", currentPlayer); + printf("\nMenu Options: \n1. Save Game and Quit to Menu\n2. Quit to Menu (No Save)\n3. Review Game (Enter Analysis Mode)\n\nChoice:"); scanf("%d",&winMenu); // Initialise Game Name @@ -208,6 +212,18 @@ void saveGameLog(Game game, char* gameName){ fclose(fp); } +void insertCPU(Game* game){ + // Generate Random Valid Column + int num = (rand() % (game->columnSize - 1 + 1)) + 1; + + // Insert Coin as Player Two if invalid try again + if(!insertCoin(game,num,2)){ + togglePlayer(game); + } else {insertCPU(game);} + + +} + // Create New Entry Structure struct Entry* newEntry(struct Entry* log, int move, int pTurn) { //Assuming log is not empty means valid game diff --git a/games.h b/games.h index be85005..0ada6e7 100644 --- a/games.h +++ b/games.h @@ -54,14 +54,14 @@ struct Entry* newEntry(struct Entry* log, int move, int pTurn); // Menu void menu(); -void cpuGame(); -void newGame(char *player1, char *player2, int columns, int rows); -void startGame(); +void newGame(int gameType, char *player1, char *player2, int columns, int rows); +void startGame(int gameType); void loadLog(char line[LINE_LENGTH]); void loadGames(); // In Game int moveController(Game* game); +void insertCPU(Game* game); void displayBoard(struct Position* board, int width); int insertCoin(Game* game, int column, int player); void saveGameLog(Game game, char* gameName); diff --git a/main.c b/main.c index e0eee88..65911f0 100644 --- a/main.c +++ b/main.c @@ -11,7 +11,7 @@ void menu(){ //system("cls"); printf("\nWelcome to Connect Four - CLI Edition!\n\nPlease select from the menu then press enter...\n\n" - "1. New Game\n2. New Game (Against CPU)\n3. Playback Game (Analysis Mode)\n0. Exit\n\nChoice: "); + "1. New Normal Game\n2. New CPU Game (Against CPU)\n3. Playback Game (Analysis Mode)\n0. Exit\n\nChoice: "); fgets(&menuSelection,8,stdin); switch (menuSelection) { @@ -19,10 +19,10 @@ void menu(){ return; break; case '1': // Normal Game - startGame(); + startGame(0); break; case '2': // CPU Game - //cpuGame(); + startGame(1); break; case '3': // Analysis Mode Replay @@ -36,18 +36,22 @@ void menu(){ } } -// Start Normal Game -void startGame(){ +// Start Specified Game Type +void startGame(int gameType){ // Initialise Variables char playerOne[20], playerTwo[20]; int boardSize; + // Get Player 1's Name printf("\nEnter Player One's Name (Max 20 Characters):"); scanf("%s",playerOne); - printf("\nEnter Player Two's Name (Max 20 Characters):"); - scanf("%s",playerTwo); + // Get Player 2's Name or Ignore if CPU Game + if(gameType==0) { + printf("\nEnter Player Two's Name (Max 20 Characters):"); + scanf("%s", playerTwo); + } // Show Menu Options printf("\n\n\nPlease select a Game Type:\n\n1. Standard Game (7x6 Board)\n2. Mini Game (4x4)\n3. Massive Game (10x10)\n4. Custom Game (Variable Board Size/Shape)\n"); @@ -57,12 +61,12 @@ void startGame(){ scanf("%d",&boardSize); // Validate Input - if(!isInRange(1,4,boardSize)){startGame();} + if(!isInRange(1,4,boardSize)){startGame(gameType);} else { - if(boardSize==1){newGame(playerOne, playerTwo,7,6);} //Normal Game - else if(boardSize==2){newGame(playerOne,playerTwo,4,4);} //Mini Game - else if(boardSize==3){newGame(playerOne,playerTwo,10,10);} //Big Game + if(boardSize==1){newGame(gameType,playerOne, playerTwo,7,6);} //Normal Game + else if(boardSize==2){newGame(gameType,playerOne,playerTwo,4,4);} //Mini Game + else if(boardSize==3){newGame(gameType,playerOne,playerTwo,10,10);} //Big Game else if(boardSize==4){ // Initialise Variables @@ -84,7 +88,9 @@ void startGame(){ if (!isInRange(2,10,selectedRow)){menu();} //Start the Game - newGame(playerOne,playerTwo,selectedCol,selectedRow); + + if (gameType==0){newGame(gameType,playerOne,playerTwo,selectedCol,selectedRow);} + else if (gameType==1){newGame(gameType,playerOne,"CPU",selectedCol,selectedRow);} } } @@ -92,11 +98,6 @@ void startGame(){ } -// Start CPU Game -void cpuGame(){ - menu(); -} - // Open Load Games Menu void loadGames(){