我正在制作由两个人类玩家玩的Connect 4游戏。我的问题是我希望用户能够为其作品输入角色,但是我的班级设置方式是该作品必须是整数,游戏才能确定您是否获胜。
input.h
#include <cstdlib>
#include <string>
#include <cstring>
#include <bits/algorithmfwd.h>
#include <cctype>
#include<bits/stdc++.h>
using namespace std;
bool validname(string name){
int x = 0;
if(name.length()==0){return false;}
for(unsigned int i = 0; i<name.length(); i++) {
char c = name[i];
if (isspace(c)){
x++;
}
else{
;
}
}
if(x==0){
return true;
}
else{
return false;
}
}
bool validchar(char name){
int x = 0;
char c = name;
if (isspace(c)){
x++;
}
else{
;
}
if(x==0){
return true;
}
else{
return false;
}
}
string nameone(){
while(true)
{
string input;
cout << "Player 1, please enter your name: " << endl;
getline(cin, input);
if(validname(input))
{
return input;
}
else{
continue;
}
}
}
char pieceone(string name){
char piece;
string input;
while(true) {
cout << name << ", please enter the character you want to use for your piece: " << endl;
getline(cin, input);
if (input.length() > 1) {
continue;
} else {
piece = input[0];
if (validchar(piece)) {
return piece;
} else {
continue;
}
}
}
}
string nametwo(string name){
while(true)
{
string input;
cout << "Player 2, please enter your name: " << endl;
getline(cin, input);
transform(name.begin(), name.end(), name.begin(), ::tolower);
transform(input.begin(), input.end(), input.begin(), ::tolower);
if(validname(input) and input != name)
{
return input;
}
else{
continue;
}
}
}
char piecetwo(string name, char oldpiece){
string input;
char piece;
char cow;
while(true) {
cout << name << ", please enter the character you want to use for your piece: " << endl;
getline(cin, input);
//cow = char(tolower(piece));
transform(input.begin(), input.end(), input.begin(), ::tolower);
if(input.length()>1 or input.length()<1)
{continue;}else {
piece = input[0];
if (validchar(piece) and piece != oldpiece) {
return piece;
} else {
continue;
}
}
}
}
ConnectN.h
#ifndef CONNECT4GAME_H
#define CONNECT4GAME_H
#include <string>
using namespace std;
class Connect4Game
{
private:
int board[7][7]; // to create a 7 * 7 board
int turn; // to toggle the turn of the players
char pieceOne;
char pieceTwo;
string nameOne;
string nameTwo;
public:
Connect4Game(); // constructor
void showBoard(); // to show board
void switchTurn(); // to toggle the turns
bool Won(); // to check whether any one win or not
bool BoardFull(); // to check whether the board is full or not
void start(); // the starter of the game
// ~Connect4Game();
};
#endif
ConnectN.cpp
#include "ConnectN.h"
#include <iostream>
#include "input.h"
using namespace std;
//Constructor
Connect4Game::Connect4Game()
{
string PlayerOneName = nameone();
char PlayerOnePiece = pieceone(PlayerOneName);
string PlayerTwoName = nametwo(PlayerOneName);
char PlayerTwoPiece = piecetwo(PlayerTwoName,PlayerOnePiece);
pieceOne = PlayerOnePiece;
pieceTwo = PlayerTwoPiece;
nameOne = PlayerOneName;
nameTwo = PlayerTwoName;
turn=1;
for(int i=0;i<7;i++) // first initialized all cell by 0
{
for(int j=0;j<7;j++)
{
board[i][j]=0;
}
}
}
// To display the board
void Connect4Game::showBoard()
{
for(int i=0;i<7;i++)
{
cout<<"\t\t";
for(int j=0;j<7;j++)
{
if(board[i][j]==0){
cout<<"*";}
else{cout<<board[i][j];}
}
cout<<endl;
}
cout<<endl;
}
// to toggle turns
void Connect4Game::switchTurn()
{
// I use 2 for the player 2 and 1 for the player 1
int mod;
turn++; // here if turn is 1 it become 2 and if turn is 2 it becomes 3
mod=turn%2; // and the mod becomes 0 if turn is 2 and becomes 1 if if turn is 3
if(mod==0) {
turn = 2; // which mean its player 2 turn
}
else {
turn = 1; // which mean its player 1 turn
}
}
// To check which player won
bool Connect4Game::Won()
{
// horizontally check whether the sum of alternate 4 column are 4 or 8 and also check all the columns are filled
for(int i=6;i>=0;i--)
{
for(int j=0;j<4;j++)
{
if( (board[i][j]+board[i][j+1]+board[i][j+2]+board[i][j+3]==8||
board[i][j]+board[i][j+1]+board[i][j+2]+board[i][j+3]==4)&&
(board[i][j]!=0&&board[i][j+1]!=0&&board[i][j+2]!=0&&board[i][j+3]!=0) )
{
if(board[i][j]+board[i][j+1]+board[i][j+2]+board[i][j+3]==8) // To check which player won // player won
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
// vertically check whether the sum of alternate 4 rows are 4 or 8 and also check all the rows are filled
for(int j=0;j<7;j++)
{
for(int i=6;i>2;i--)
{
if( (board[i][j]+board[i-1][j]+board[i-2][j]+board[i-3][j]==8
||board[i][j]+board[i-1][j]+board[i-2][j]+board[i-3][j]==4)&&
(board[i][j]!=0&&board[i-1][j]!=0&&board[i-2][j]!=0&&board[i-3][j]!=0) )
{
if(board[i][j]+board[i-1][j]+board[i-2][j]+board[i-3][j]==8)// To check which // player won
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
// diagnally check whether the sum of diagonal 4 rows are 4 or 8 and also check all the 4 cell are filled
for(int i=6;i>2;i--)
{
for(int j=0;j<4;j++)
{
if(j==0&&i==6)
{
for(int c=0;c<4;c++)
{
if((board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==8||
board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==4)&&
(board[i-c][j+c]!=0&&board[i-(c+1)][j+(c+1)]!=0&&board[i-(c+2)][j+(c+2)]!=0&&board[i-(c+3)][j+(c+3)]!=0))
{
if(board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<< " won !\n";
return true;
}
}
}
}
if((j==1&&i==6)||(j==0&&i==5))
{
for(int c=0;c<3;c++)
{
if((board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==8||
board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==4)&&
(board[i-c][j+c]!=0&&board[i-(c+1)][j+(c+1)]!=0&&board[i-(c+2)][j+(c+2)]!=0&&board[i-(c+3)][j+(c+3)]!=0))
{
if(board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
if(j==2&&i==6||(j==0&&i==4))
{
for(int c=0;c<2;c++)
{
if((board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==8||
board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==4)&&
(board[i-c][j+c]!=0&&board[i-(c+1)][j+(c+1)]!=0&&board[i-(c+2)][j+(c+2)]!=0&&board[i-(c+3)][j+(c+3)]!=0))
{
if(board[i-c][j+c]+board[i-(c+1)][j+(c+1)]+board[i-(c+2)][j+(c+2)]+board[i-(c+3)][j+(c+3)]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
if(i==6&&j==3||(j==0&&i==3))
{
if((board[i][j]+board[i-1][j+1]+board[i-2][j+2]+board[i-3][j+3]==8||
board[i][j]+board[i-1][j+1]+board[i-2][j+2]+board[i-3][j+3]==4)&&
(board[i][j]!=0&&board[i-1][j+1]!=0&&board[i-2][j+2]!=0&&board[i-3][j+3]!=0))
{
if(board[i][j]+board[i-1][j+1]+board[i-2][j+2]+board[i-3][j+3]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
}
for(int i=6;i>2;i--)
{
for(int j=6;j>2;j--)
{
if(j==6&&i==6)
{
for(int c=0;c<4;c++)
{
if((board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==8||
board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==4)&&
(board[i-c][j-c]!=0&&board[i-(c+1)][j-(c+1)]!=0&&board[i-(c+2)][j-(c+2)]!=0&&board[i-(c+3)][j-(c+3)]!=0))
{
if(board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
if((j==5&&i==6)||(j==6&&i==5))
{
for(int c=0;c<3;c++)
{
if((board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==8||
board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==4)&&
(board[i-c][j-c]!=0&&board[i-(c+1)][j-(c+1)]!=0&&board[i-(c+2)][j-(c+2)]!=0&&board[i-(c+3)][j-(c+3)]!=0))
{
if(board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
if(j==4&&i==6||(j==6&&i==4))
{
for(int c=0;c<2;c++)
{
if((board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==8||
board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==4)&&
(board[i-c][j-c]!=0&&board[i-(c+1)][j-(c+1)]!=0&&board[i-(c+2)][j-(c+2)]!=0&&board[i-(c+3)][j-(c+3)]!=0))
{
if(board[i-c][j-c]+board[i-(c+1)][j-(c+1)]+board[i-(c+2)][j-(c+2)]+board[i-(c+3)][j-(c+3)]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
if(i==6&&j==3||(j==6&&i==3))
{
if((board[i][j]+board[i-1][j-1]+board[i-2][j-2]+board[i-3][j-3]==8||
board[i][j]+board[i-1][j-1]+board[i-2][j-2]+board[i-3][j-3]==4)&&
(board[i][j]!=0&&board[i-1][j-1]!=0&&board[i-2][j-2]!=0&&board[i-3][j-3]!=0))
{
if(board[i][j]+board[i-1][j-1]+board[i-2][j-2]+board[i-3][j-3]==8)
{
cout<<nameTwo<<" won !\n";
return true;
}
else
{
cout<<nameOne<<" won !\n";
return true;
}
}
}
}
}
return false;
}
// check wether the board is full or not
bool Connect4Game::BoardFull()
{
static int turnCount=0;
turnCount++;
if(turnCount==49)
{
cout<<"The match is draw /n";
return true;
}
return false;
}
void Connect4Game::start()
{
bool flag=true;
showBoard();
while(flag==true)
{
int columno=0;
bool flag2=true;
do // take input from user
{
if(turn==1){
cout<<nameOne<<", please enter a column to play in: ";
cin>>columno;}
else{
cout<<nameTwo<<", please enter a column to play in: ";
cin>>columno;}
}
while(columno<1||columno>7);
columno--;
for(int i=6;i>=0&&flag2==true;i--) // put the respective label in the desired cell
{
if(board[i][columno]==0)
{
if(turn==1){
board[i][columno]=1;
flag2=false;}
else{
board[i][columno]=2;
flag2=false;
}
}
}
if(flag2==true)
cout<<"The board has not column no"<<columno<<" available\n";
showBoard(); // show the board
flag=Won(); // check wether any player win or not
if(flag==false) // if no one win
{
flag=BoardFull(); //check board status
if(flag==false) // if board is not full
{
switchTurn(); // toggle turn
flag=true;
}
else
flag=false;
}
else
flag=false;
}
}
main.cpp
#include <iostream>
#include "ConnectN.h"
int main()
{
Connect4Game game;
game.start();
return 0;
}
我真的对编程特别是C ++还是陌生的,所以我的代码可能不太清楚。我正在尝试使其能够在用户位置上显示用户输入的棋子(pieceOne和pieceTwo),同时还能够计算连续棋子的数量,从而可以显示获胜者。现在,如果游戏使用整数作为棋子,则可以运行,但是如果我将board [i] [j]更改为char并使用用户输入的棋子,它将无限进行。我如何才能将连续的棋子数量计算为整数,但在棋盘上显示字符?
谢谢。