Skip to content

Commit

Permalink
Add Player2 Reset and final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
4FranJonas2 committed Jul 31, 2024
1 parent 95c5b1a commit 7011dca
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 117 deletions.
6 changes: 1 addition & 5 deletions Final_FranciscoJonas/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const int HEIGHT = 50;
const int MAX_ROWS = 40;
const int MAX_COLS = 20;

//cleaner size
const int CLEAN_ROWS = 30;
const int CLEAN_COLS = 30;

//arena draw position
const int arenaDrawPosX = 40;
const int arenaDrawPosY = 5;
Expand All @@ -32,7 +28,7 @@ const int arenaDrawPosY = 5;
const int white = 255;
const int orange = 97;
const int transparent = 7;
const int GREEN = 42;
const int RED = 204;
const int BLUE = 153;

//font
Expand Down
58 changes: 39 additions & 19 deletions Final_FranciscoJonas/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ void DrawPlayerCell(char characterToDraw)

void DrawWallCell(char characterToDraw)
{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), orange);
cout << characterToDraw;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), transparent);
}

void DrawColorCell(char characterToDraw)
void DrawColorCell(int characterToDraw)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BLUE);
cout << characterToDraw;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), characterToDraw);
cout << noneChar;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), transparent);
}

void DrawMatrixCell(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player)
void DrawMatrixCellPlayer1(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player)
{
for (int i = 0; i < MAX_ROWS; i++)
{
Expand All @@ -34,7 +33,7 @@ void DrawMatrixCell(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player)
DrawPlayerCell(white);
}

else if (matrix[i][j].type == CellType::COLOR)
else if (matrix[i][j].type == CellType::COLORP1)
{
Gotoxy(arenaDrawPosX + i, arenaDrawPosY + j);
DrawColorCell(BLUE);
Expand All @@ -43,6 +42,27 @@ void DrawMatrixCell(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player)
}
}

void DrawMatrixCellPlayer2(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player)
{
for (int i = 0; i < MAX_ROWS; i++)
{
for (int j = 0; j < MAX_COLS; j++)
{
if (matrix[i][j].type == CellType::PLAYER)
{
Gotoxy(arenaDrawPosX + i, arenaDrawPosY + j);
DrawPlayerCell(white);
}

else if (matrix[i][j].type == CellType::COLORP2)
{
Gotoxy(arenaDrawPosX + i, arenaDrawPosY + j);
DrawColorCell(RED);
}
}
}
}

void DrawGamePlayMenu(Player& player)
{
Gotoxy(menuPosX, menuPosY);
Expand All @@ -65,13 +85,10 @@ void DrawGamePlayMenu(Player& player)
cout << " Press ESC to back MENU." << endl;
cout << " Press R to RESUME game." << endl;
cout << "\n\n" << endl;

}

void Draw(Matrix matrix[MAX_ROWS][MAX_COLS], Player& auxPlayer)
{
system("cls");

for (int i = 0; i < MAX_ROWS; i++)
{
for (int j = 0; j < MAX_COLS; j++)
Expand All @@ -92,7 +109,7 @@ void Draw(Matrix matrix[MAX_ROWS][MAX_COLS], Player& auxPlayer)
DrawWallCell(noneChar);
break;

case CellType::COLOR:
case CellType::COLORP1:
DrawColorCell(BLUE);
break;

Expand All @@ -105,15 +122,7 @@ void Draw(Matrix matrix[MAX_ROWS][MAX_COLS], Player& auxPlayer)
}

void DrawCredits()
{
/*string creditsMenu[MAX_ROWS] = { " CREDITS ","Made by FRANCISCO JONAS"," FINAL INTEGRADOR"," Press ESC to go back..." };
for (int i = 0; i < MAX_ROWS; i++)
{
Gotoxy(arenaDrawPosX, arenaDrawPosY+i);
cout << creditsMenu[i];
}*/

{
Gotoxy(0, arenaDrawPosY);

cout << " CREDITS " << endl;
Expand Down Expand Up @@ -158,4 +167,15 @@ void DrawRules()
cout << " If you hit a Colored line or the other player, you lose." << endl;
cout << "\n";
cout << " Press ESC to go back..." << endl;
}

void DrawResetMenu()
{
Gotoxy(0, arenaDrawPosY + MAX_COLS);

cout << " MATCH ENDS " << endl;
cout << "\n\n";
cout << " Press T to reset and continue playing." << endl;
cout << "\n";
cout << " Press ESC to go back... " << endl;
}
8 changes: 5 additions & 3 deletions Final_FranciscoJonas/Draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ enum class MENU

void DrawPlayerCell(char characterToDraw);
void DrawWallCell(char characterToDraw);
void DrawColorCell(char characterToDraw);
void DrawMatrixCell(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player);
void DrawColorCell(int characterToDraw);
void DrawMatrixCellPlayer1(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player);
void DrawMatrixCellPlayer2(Matrix matrix[MAX_ROWS][MAX_COLS], Player& player);
void DrawGamePlayMenu(Player& player);
void Draw(Matrix matrix[MAX_ROWS][MAX_COLS], Player& auxPlayer);
void DrawCredits();
void DrawMainMenu();
void DrawRules();
void DrawRules();
void DrawResetMenu();
40 changes: 28 additions & 12 deletions Final_FranciscoJonas/GamePlay.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once
#include "GamePlay.h"

DIRECTION playerDir;
Player player;
Matrix matrix[MAX_ROWS][MAX_COLS];
MainMenu mainMenu;
SimulationStatus simulation;
MENU menu;

//main loop del juego
void GameLoop()
{
DIRECTION playerDir;
DIRECTION playerDir2;
Player player;
Player player2;
Matrix matrix[MAX_ROWS][MAX_COLS];
MainMenu mainMenu;
SimulationStatus simulation;
MENU menu;

srand(time(0));
// simulation loop
do
Expand All @@ -26,22 +28,36 @@ void GameLoop()

matrixSetUp(matrix);
playerSetUp(player, matrix, player.initPlayerPosX, player.initPlayerPosY, playerDir);
playerSetUp(player2, matrix, player2.initPlayer2PosX, player2.initPlayer2PosY, playerDir2);

//pause loop
while (!simulation.pauseStatus)
{
Draw(matrix, player);
DrawGamePlayMenu(player);

//gameplay loop
while (!player.gameOver)
while (!player.gameOver && !player2.gameOver)
{
InputInGame(player, matrix, playerDir);
GameLogic(player, playerDir, matrix);
InputPlayer1(player, matrix, playerDir);
InputPlayer2(player2, matrix, playerDir2);
GameLogicpPayer1(player, playerDir, matrix);
GameLogicpPayer2(player2, playerDir2, matrix);
DrawGamePlayMenu(player);
DrawMatrixCell(matrix, player);
DrawMatrixCellPlayer1(matrix, player);
DrawMatrixCellPlayer2(matrix, player2);
Sleep(50);
}

UpdatePause(player, simulation, mainMenu, menu);
if (!player.playerIsAlive || !player2.player2IsAlive)
{
DrawResetMenu();
UpdateTryAgain(player, player2, simulation, mainMenu, menu, matrix, playerDir, playerDir2);
}
else
{
UpdatePause(player, player2, simulation, mainMenu, menu);
}
}
} while (!simulation.menuStatus);
} while (!simulation.endSimulation);
Expand Down
79 changes: 78 additions & 1 deletion Final_FranciscoJonas/Input.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Input.h"

void InputInGame(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS],DIRECTION& playerDir)
void InputPlayer1(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS], DIRECTION& playerDir)
{
if (_kbhit())
{
Expand Down Expand Up @@ -34,6 +34,44 @@ void InputInGame(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS],DIRECTION& pl
default:
break;
}


}
}

void InputPlayer2(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS], DIRECTION& playerDir)
{
int possibleMoves = 4;
int move = 0;
int lastMove = 0;

move = rand() % possibleMoves;
move = CheckBotMove(move, lastMove);

switch (move)
{
case 1:
playerDir = DIRECTION::LEFT;
lastMove = move;
break;

case 2:
playerDir = DIRECTION::RIGHT;
lastMove = move;
break;

case 3:
playerDir = DIRECTION::UP;
lastMove = move;
break;

case 4:
playerDir = DIRECTION::DOWN;
lastMove = move;
break;

default:
break;
}
}

Expand Down Expand Up @@ -72,4 +110,43 @@ void InputInMenu(MENU& menu)
break;
}
}
}

int CheckBotMove(int move, int lastMove)
{
int possibleMoves = 4;
int newMove = 0;

bool upNoDown = (lastMove == (move - 1));
bool downNoUp = (lastMove == (move + 1));
bool leftNoRight = (lastMove == (move - 1));

if (upNoDown)
{
do
{
newMove = rand() % possibleMoves;

} while (!upNoDown);
}

else if (downNoUp)
{
do
{
newMove = rand() % possibleMoves;

} while (!downNoUp);
}

else if (leftNoRight)
{
do
{
newMove = rand() % possibleMoves;

} while (!leftNoRight);
}

return newMove;
}
7 changes: 4 additions & 3 deletions Final_FranciscoJonas/Input.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "Update.h"

void InputInGame(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS], DIRECTION& playerDir);

void InputInMenu(MENU& menu);
void InputPlayer1(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS], DIRECTION& playerDir);
void InputPlayer2(Player& player, Matrix matrix[MAX_ROWS][MAX_COLS], DIRECTION& playerDir);
void InputInMenu(MENU& menu);
int CheckBotMove(int move, int lastMove);
1 change: 0 additions & 1 deletion Final_FranciscoJonas/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#include "GamePlay.h"


using namespace std;

void main()
Expand Down
3 changes: 2 additions & 1 deletion Final_FranciscoJonas/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ enum class CellType
{
NONE = 1,
PLAYER,
COLOR,
COLORP1,
COLORP2,
WALL
};

Expand Down
3 changes: 1 addition & 2 deletions Final_FranciscoJonas/Player.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "Player.h"



Player playerSetUp(Player& auxPlayer, Matrix matrix[MAX_ROWS][MAX_COLS], int spawnPosX, int spawnPosY, DIRECTION& playerDir)
{
auxPlayer.gameOver = false;
bool pauseStatus = false;
bool menuStatus = false;
bool endSimulation = false;
auxPlayer.playerIsAlive = true;
playerDir = DIRECTION::STOP;

//spawn point
Expand Down
Loading

0 comments on commit 7011dca

Please sign in to comment.