I'm trying to pass a command line argument into my C++ file that determines the size of a matrix for the game Reversi. For the life of me I can't figure out why it's not working.
#include <iostream>
#include <cstdlib>
using namespace std;
const char player1='X';
const char player2='O';
struct Dimension
{
int size;
};
char PrintBoard(char board[][mat.size]);
char PrintBoard(char board[][mat.size]){
//if(boardSize % 2 == 0 && boardSize >= 4 && boardSize <= 10){
for(int i = 0; i < mat.size; i++){
for(int j = 0; j < mat.size; j++){
board[i][j]='-';
}
}
for(int i = 0;i < mat.size; i++){
for(int j = 0; j < mat.size; j++){
cout<<board[i][j];
}
cout<<endl;
}
return board[mat.size][mat.size];
}
//else{
//cout<<"The board must be an even size between 4 and 10."<<endl;
//}
int main(int argc, char*argv[]){
Dimension mat;
mat.size=atoi(argv[1]);
char board[mat.size][mat.size];
board[mat.size][mat.size]=PrintBoard(board);
int i=1;
while(i<mat.size*mat.size){
char pawn;
char oppo;
if(i%2 != 0){
pawn=player1;
oppo=player2;
}
else
{
pawn=player2;
oppo=player1;
}
cout<<pawn<<"'s turn: "<<endl;
cin>>row>>col;
board[row-1][col-1]=pawn;
//RIGHT
if(board[row-1][col]==oppo && board[row-1][col+1] != '-' && board[row-1][col+1] != pawn)
{
int c=col;
while(board[row-1][c]!=pawn && board[row-1][c] != '-')
{
board[row-1][c]=pawn;
c++;
}
}
//LEFT
if(board[row-1][col-2]==oppo && board[row-1][col-3] != '-' && board[row-1][col-3] != pawn)
{
int c=col-2;
while(board[row-1][c]!=pawn && board[row-1][c] != '-')
{
board[row-1][c]=pawn;
c--;
}
}
//UP
if(board[row-2][col-1]==oppo && board[row-3][col-1] != '-' && board[row-3][col-1] != pawn)
{
int r=row-2;
while(board[r][col-1]!=pawn && board[r][col-1] != '-')
{
board[r][col-1]=pawn;
r--;
}
}
//DOWN
if(board[row][col-1]==oppo && board[row+1][col-1] != '-' && board[row+1][col-1] != pawn)
{
int r=row;
while(board[r][col-1]!=pawn && board[r][col-1] != '-')
{
board[r][col-1]=pawn;
r++;
}
}
//DOWN-RIGHT
if(board[row][col]==oppo && board[row+1][col+1] != '-' && board[row+1][col+1] != pawn)
{
int r=row;
int c=col;
while(board[r][c] != pawn && board[r][c] != '-' && r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
{
board[r][c]=pawn;
r++;
c++;
}
}
//DOWN-LEFT
if(board[row][col-2]==oppo && board[row+1][col-3] != '-' && board[row+1][col-3] != pawn)
{
int r=row;
int c=col-2;
while(board[r][c] != pawn && board[r][c] !='-' && r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
{
board[r][c]=pawn;
r++;
c--;
}
}
//UP-RIGHT
if(board[row-2][col]==oppo && board[row-3][col+1] != '-' && board[row-3][col+1] != pawn)
{
int r=row-2;
int c=col;
while(board[r][c] != pawn && board[r][c] !='-'&& r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
{
board[r][c]=pawn;
r--;
c++;
}
}
//UP-LEFT
if(board[row-2][col-2]==oppo && board[row-3][col-3] != '-' && board[row-3][col-3] != pawn)
{
int r=row-2;
int c=col-2;
while(board[r][c] != pawn && board[r][c] !='-' && r-1 != -1 && c-1 != -1 && r != mat.size+1 and c != mat.size+1)
{
board[r][c]=pawn;
r--;
c--;
}
}
//PRINTTTT
for(int h = 0;h < mat.size; h++){
for(int j = 0; j < mat.size; j++){
cout<<board[h][j];
}
cout<<endl;
}
i++;
}
int o_num=0;
int x_num=0;
for(int r=0; r<mat.size; r++)
{
for(int c=0; c<mat.size; c++)
{
if(board[r][c]=='O')
o_num++;
else if(board[r][c]=='X')
x_num++;
else
;
}
}
cout<<"Number of O's: "<<o_num<<endl;
cout<<"Number of X's: "<<x_num<<endl;
if(o_num>x_num)
cout<<"O Wins!"<<endl;
else if(o_num<x_num)
cout<<"X Wins!"<<endl;
else
cout<<"Tie!"<<endl;
return 0;
}
If I change mat.size
to a const
like ROWS
it works perfectly, but only when I try to make it a command line argument it complains. Can somebody help find the problem? I'm almost at my wits end. Thank you so much in advanced!
EDIT: This is the compiler error. Sorry should have added this before.
board.cpp:14:30: error: ‘mat’ was not declared in this scope
char PrintBoard(char board[][mat.size]);
^
board.cpp:17:30: error: ‘mat’ was not declared in this scope
char PrintBoard(char board[][mat.size]){
^
board.cpp: In function ‘char PrintBoard(...)’:
board.cpp:20:23: error: ‘mat’ was not declared in this scope
for(int i = 0; i < mat.size; i++){
^
board.cpp:22:7: error: ‘board’ was not declared in this scope
board[i][j]='-';
^
board.cpp:25:21: error: ‘mat’ was not declared in this scope
for(int i = 0;i < mat.size; i++){
^
board.cpp:27:13: error: ‘board’ was not declared in this scope
cout<<board[i][j];
^
board.cpp:32:10: error: ‘board’ was not declared in this scope
return board[mat.size][mat.size];
^
board.cpp:32:16: error: ‘mat’ was not declared in this scope
return board[mat.size][mat.size];
^
board.cpp: In function ‘int main(int, char**)’:
board.cpp:61:10: error: ‘row’ was not declared in this scope
cin>>row>>col;
^
board.cpp:61:15: error: ‘col’ was not declared in this scope
cin>>row>>col;
^
board.cpp: In function ‘char PrintBoard(...)’:
board.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type ]
}
^
Arrays need to have constant sizes, determined at compile-time. To make it work, you will need to use dynamic memory allocation with new
. Instead of char board[mat.size][mat.size]
, you can use char* board=new char[mat.size*mat.size]
.
However, this will give you a one-dimensional array. To get two-dimensional coordinates to a one-dimensional coordinate, this is the formula: int idx=(mat.size*y)+x;
Therefore, instead of board[x][y]
, you will need to use board[mat.size*y+x]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.