我正在尝试构建一个2人游戏,程序要掷骰子,玩家必须从12列中选择要玩的列。
我的问题是骰子滚动时(例如:5),玩家1选择他们的专栏。 工作正常,直到播放器2选择同一列并覆盖播放器1在该列中的内容,而不是在其上方的可用空间中播放。
这是我的代码:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <iomanip>
#include <algorithm>
#include "TheRebuild.h"
using namespace std;
int k = 0;
auto dice = 0;
int col_in = 0;
int col_space = 0;
int x = 0;
int y = 0;
int counter = 0;
char space = '-';
char board[gridrow][gridcol];
//accessors
string Players::getname(Users play[],int k) const {
return play[k].name;
}
char Players::gettoken(Users play[],int k) const {
return play[k].token;
}
int Players::getscore(Users play[],int k) const {
return play[k].score;
}
//mutator
void Players::RecInfo(Users play[],int k,string id,char piece,int scr) {
play[k].name = id;
play[k].token = piece;
play[k].score = scr;
}
Players::Players() {
for (int k = 0; k < arrsize; k++) {
play[k].name = "";
play[k].token = ' ';
play[k].score = 0;
}
}
void Players::askUser(Players::Users *play)
{
string id = " ";
char piece = ' ';
int scr = 0;
//cin.get();
for (int k = 0; k <= arrsize; k++) {
cout << "Player " << k + 1 << "! Enter your Name: ";
getline(cin,id);
cout << "Player " << k + 1 << " Enter a character to play with: ";
cin >> piece;
cout << endl;
system("pause");
RecInfo(play,k,id,piece,scr);
system("cls");
cin.get();
}
}
int Players::DiceRoll()
{
int dice = 0;
srand(time(nullptr));
dice = (rand() % 6) + 1;
return dice;
}
void Players::Player1() {
dice = DiceRoll();
cout << "Dice stopped rolling at: " << dice << endl;
cout << play[0].name <<" Enter column to play in: ";
cin >> col_in;
if ((col_in < 1) && (col_in > 12)) // failsafe - player does not play anything out of 1-12 range
{
cin.get();
cout << "Turn lost!";
return;
}
else
{
col_space = col_in-1;
}
if (board[x][gridcol] != space)
{
x++;
for (x = gridcol; x > gridcol - (dice +1); x--) // this loop allows player pieces to start at "11" of the board rather than at the "0"
{
board[x][col_space] = play[0].token;
Sleep(50);
}
}
else
{
for (x = gridcol; x > gridcol - (dice +1); x--) // this loop allows player pieces to start at "11" of the board rather than at the "0"
{
board[x][col_space] = play[0].token;
Sleep(50);
}
}
}
void Players::Player2() {
dice = DiceRoll();
cout << "Dice stopped rolling at: " << dice << endl;
cout << play[1].name <<" Enter column to play in: ";
cin >> col_in;
if ((col_in < 1) && (col_in > 12)) // failsafe - player does not play anything out of 1-12 range
{
cin.get();
cout << "Turn lost!";
return;
}
else
{
col_space = col_in - 1;
}
for (x = gridcol; x > gridcol-(dice +1); x--) // this loop allows player pieces to start at the bottom of the board rather than at the top
{
board[x][col_space] = play[1].token;
Sleep(50);
}
}
void Players::Header() {
//---------------------------- Print --------------------------------
// this prints the board with all the changes made by the player... this is OK
string border(48,'-');
system("cls");
cout << border << endl
<< "| Fill 'Em Up | \n"
<< border << endl
<< " 1 2 3 4 5 6 7 8 9 10 11 12 \n"
<< border << endl; // header for the board
}
void Players::Sub_UI() {
//Basic UI elements - Displays Player Name,Their Score (number of tokens on the board),and the token they chose
// I plan to add colors for player 1 and player 2 tokens,but the gameplay is more important
string sub(15,'^');
cout << endl << sub << "\t\t\t " << sub << endl; // top boarder for player information
cout << "Player: " << play[0].name << "\t\t\t" << " Player: " << play[1].name << endl; // names of players
}
bool Players::endgame() {
return false;
}
void Players::mainStage(Players::Users *play) {
for(x = 0; x <gridrow; x++){
for (y = 0; y < gridcol; y++){
board[x][y] = space;
}
}
do // loop for the whole game - see "while" at the end
{
Header();
for (x = 0; x < gridrow; x++) // actual grid being printed x is row,y is col,just like a normal x and y axis
{
for (y = 0; y < gridcol; y++) {
cout << " " << board[x][y] << " ";
}
cout << endl;
}
//Token Counter
int count1 = count(*board,*board+144,play[0].token); // used to count how many tokens on the board belong to Player 1
int count2 = count(*board,play[1].token); // used to count how many tokens on the board belong to Player 2
//Small UI elements
Sub_UI();
cout << "Score: " << count1 << " -> " << play[0].token << "\t\t\t" << " Score: " << count2 << " -> " << play[1].token << endl << endl; // scores and tokens respectively
//Player1
if (counter % 2 == 0)
{
Player1();
}
else //Player2
{
Player2();
}counter++;
}while(!endgame());
}
有人对此有任何想法吗?
完成后应该看起来像这样
------------------------------------------------
| Game Title |
------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12
------------------------------------------------
- - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - -
- $ - - - - - - $ - - -
- $ - - - - - - $ - - -
- $ - - - - - - $ - - -
- $ - - - - - - @ - - @
- $ - - - - - - @ - - @
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
Player: Player1 Player: Player2
Score: 4 -> @ Score: 8 -> $
Dice stopped rolling at: 2
Player1 Enter column to play in:
请注意玩家2如何输入上方 3个令牌,而不是覆盖它们