1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <conio.h>
using namespace std;
void outputBoard(int playerRow, int playerColumn);
char board [8] [21];
char entrance[3], exit[3];
int playerRow = 0, playerColumn = 11;
int numOfMoves = 1;
int playerMove(int playerRow, int playerColumn, char entrance[3]);
int main()
{
outputBoard(playerRow, playerColumn);
do{
playerMove(playerRow, playerColumn, entrance);
outputBoard(playerRow, playerColumn);
++numOfMoves;
}while(playerRow < 9);
return 0;
}
void outputBoard(int playerRow, int playerColumn)
{
if (numOfMoves == 1)
{
entrance[1] = 'O';
}
else
{
board[playerRow] [playerColumn] = 'O';
}
cout << " ---------"<<exit[0]<<exit[1]<<exit[2]<<"---------\n";
for (int x = 0; x<8; ++x)
{
cout << "|";
for (int y = 0; y<21; ++y)
{
cout << board[x] [y];
}
cout << "|\n";
}
cout << " ---------"<<entrance[0]<<entrance[1]<<entrance[2]<<"---------\n";
if (numOfMoves == 1)
{
entrance[1] = ' ';
}
else
{
board [playerRow] [playerColumn] = ' ';
}
}
int playerMove (int playerRow, int playerColumn, char entrance[3])
{
bool validInput = true;
char playerMoveChoice;
do
{
playerMoveChoice = getch();
if (playerMoveChoice == 'W' || playerMoveChoice == 'w')
{
++playerRow;
}
else if ((playerMoveChoice == 'A' || playerMoveChoice == 'a') && playerColumn > 0)
{
if(numOfMoves == 0)
{
entrance[0] = 'O';
}
else
{
--playerColumn;
}
}
else if ((playerMoveChoice == 'D' || playerMoveChoice == 'd') && playerColumn < 22)
{
if (numOfMoves == 0)
{
entrance[2] = 'O';
}
else
{
++playerColumn;
}
}
else if ((playerMoveChoice == 'S' || playerMoveChoice == 's') && numOfMoves != 0)
{
--playerRow;
}
else
{
cout << "Invalid input, please re-input.\n";
validInput = false;
}
}while(!validInput);
return 0;
}
|