Skip to content

Commit

Permalink
Added Play Against CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiswatson55 committed Apr 24, 2021
1 parent 80b8c29 commit 132ab5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
3 changes: 2 additions & 1 deletion gamedata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
24 changes: 20 additions & 4 deletions games.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
};

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions games.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 18 additions & 17 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ 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) {
case '0': // Exit
return;
break;
case '1': // Normal Game
startGame();
startGame(0);
break;
case '2': // CPU Game
//cpuGame();
startGame(1);
break;

case '3': // Analysis Mode Replay
Expand All @@ -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");
Expand All @@ -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
Expand All @@ -84,19 +88,16 @@ 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);}

}
}


}

// Start CPU Game
void cpuGame(){
menu();
}

// Open Load Games Menu
void loadGames(){

Expand Down

0 comments on commit 132ab5b

Please sign in to comment.