int gameBoard[3][3];
by the "N" that you read from the user. And change signature of all the functions to int ** instead of [3][3] for example bool isBoardSolved(int board[3][3])
should be bool isBoardSolved(int **board)
|
|
i need to convert this program into an N by N, so that when the user inputs a number it will create a slide tile accordingly. for example: when the user enters 5 , it will create 5 x 5 slide tile. my code: #include <iostream> #include <Windows.h> #include <conio.h> #include <time.h> #include <iomanip> using namespace std; void printBoard(int board[3][3]); void initializeBoard(int board[3][3]); void slideTile(int board[3][3],int move); bool isBoardSolved(int[3][3]); void scrambleBoard(int[3][3]); int main() { int gameBoard[3][3]; char input; bool invalid = false; initializeBoard(gameBoard); //init printBoard(gameBoard); //print cout << boolalpha; cout<<"isBoardSolved(): "<<isBoardSolved(gameBoard)<<endl; cout<<"Press enter to begin"<<endl; cin.get(); //wait cout<<"Scrambling board..."<<endl; scrambleBoard(gameBoard); //scramble cout<<"Scrambling complete, press enter to continue"<<endl; cin.get(); //wait system("CLS"); printBoard(gameBoard); cout<<endl<<endl; cout<<"[W - Up] [S - Down] [A - Left] [D - Right]"<<endl; cout<<"Input: "; while(!isBoardSolved(gameBoard)) //Game loop { input = getch(); system("CLS"); switch(toupper(input)) //input move { case 'W': slideTile(gameBoard,2); break; case 'A': slideTile(gameBoard,0); break; case 'S': slideTile(gameBoard,3); break; case 'D': slideTile(gameBoard,1); break; default: invalid = true; } printBoard(gameBoard); cout<<endl<<endl; cout<<"[W - Up] [S - Down] [A - Left] [D - Right]"<<endl; if(invalid) { cout<<"Invalid Input - ["<<input<<"]"; invalid = false; } else cout<<"Last Input: "<<input; } cout<<endl; cout<<"BOARD SOLVED"<<endl; system("PAUSE"); return 0; } /*Print board and replace zero with star*/ void printBoard(int board[3][3]) { HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); for(int row = 0; row < 3; row++) { for(int column = 0; column < 3; column++) { if(board[row][column] == 0) { SetConsoleTextAttribute(hConsole, 7); //Default color cout<<" *"; } else { if(board[row][column] == ((row*3)+(column+1))) //If the number is equal to the correct number (row*3)+(Col+1) then set to green SetConsoleTextAttribute(hConsole, 10); else SetConsoleTextAttribute(hConsole, 12); //else, set to red cout<<" "<<board[row][column]; } } cout<<endl; } SetConsoleTextAttribute(hConsole, 7); } /*Set up board*/ void initializeBoard(int board[3][3]) { int i = 1; for(int row = 0; row < 3; row++) { for(int column = 0; column < 3; column++) { board[row][column] = i; i++; } board[2][2] = 0; } } /*Make a move*/ void slideTile(int board[3][3],int move) { int emptyRow; int emptyCol; int emptySpace[2]; bool legalMoves[4] = {1,1,1,1}; //array of legal moves, [0] = left, [1] = right, [2] = up, [3] = down. true (1) indicates a legal move. for(int row = 0; row < 3; row++) { for(int column = 0; column < 3; column++) { if(board[row][column] == 0) //Find location of empty space { emptyRow = row; emptyCol = column; } } } if(emptyRow + 1 > 2) //Can i move up? legalMoves[2] = false; //If no, set move flag to false else if(emptyRow - 1 < 0) //Move down? legalMoves[3] = false; if(emptyCol - 1 < 0) //Move right? legalMoves[1] = false; else if(emptyCol + 1 > 2) //Move left? legalMoves[0] = false; switch(move) //Repleace zero space with space to the left right up or down. { case 0: if(legalMoves[move]) { board[emptyRow][emptyCol] = board[emptyRow][emptyCol + 1]; board[emptyRow][emptyCol + 1] = 0; emptyCol = emptyCol+1; } break; case 1: if(legalMoves[move]) { board[emptyRow][emptyCol] = board[emptyRow][emptyCol - 1]; board[emptyRow][emptyCol- 1] = 0; emptyCol = emptyCol-1; } break; case 2: if(legalMoves[move]) { board[emptyRow][emptyCol] = board[emptyRow+1][emptyCol]; board[emptyRow+1][emptyCol] = 0; emptyRow = emptyRow+1; } break; case 3: if(legalMoves[move]) { board[emptyRow][emptyCol] = board[emptyRow-1][emptyCol]; board[emptyRow-1][emptyCol] = 0; emptyRow = emptyRow-1; } break; } } bool isBoardSolved(int board[3][3]) { int solvedBoard[3][3] = {{1,2,3},{4,5,6},{7,8,0}}; bool boardSolved = true; int row = 0; int col = 0; while(boardSolved && row<=2) { if(solvedBoard[row][col] == board[row][col]) //Compare each index of solved board with game board { col++; if(col >= 3) { row++; col = 0; } } else //Once a discrepancy is found, set boardSolved to false to break the loop boardSolved = false; } return boardSolved; } void scrambleBoard(int board[3][3]) { srand ( time(NULL) ); int move; while(isBoardSolved(board)) //If the board ends up being solved at the end of the scramble, scramble the board again { for(int i = 0; i < 100000;i++) //Series of random moves { move = rand() % 4; slideTile(board,move); } } } i want to create a N by N. just need help with how to do it. |