1D array to 2D array conversion.

Okay so I have wrote this program that is like a mini-game. These were the requirements:
Write a program that displays a 5 by 5 grid of dashes with an X that can be moved around the grid.

Create 5 arrays with 5 elements each
Create a "reset" function that sets all the elements in the 5 arrays to "-".
Create a function to display the grid.
Add a menu like the test that allows the user to choose from the following commands: Move Left, Move Right, Move Up, Move Down, Reset, Exit.
Create two variables to hold the location of an "X". These variables will keep track of the row and column of the "X". Initially set the row and column to 0.
When the user selects one of the commands the "X" should move and the grid redisplayed. You should have a function that updates the grid based on the location of the "X".


Here is my current code:
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
98
99
100
101
102
103
104
105
106
107
108
109

#include "stdafx.h" 
#include <iostream>
#include <math.h> //needed for  pow and other math functions
#include <time.h> //needed for  time(NULL)
using namespace std ;
//global variables
char row1[] =  { '-','-','-', '-','-'} ;
char row2[] =  { '-','-','-', '-','-'} ;
char row3[] =  { '-','-','-', '-','-'} ;
char row4[] =  { '-','-','-', '-','-'} ;
char row5[] =  { '-','-','-', '-','-'} ;
int arrSize = 5 ;
//Resets the board
void reset() { //Resets the board

	for (int i = 0 ; i < arrSize ; i++) { 
	
		row1[i] = '-' ;
		row2[i] = '-' ;
		row3[i] = '-' ;
		row4[i] = '-' ;
		row5[i] = '-' ;
	}
	cout  <<  "The game has been reset"  ;
}
//displays the board
void disBoard() { 
	
	for (int i = 0  ; i < arrSize  ; i++) { 
		cout << row1[i] ;
	}
	cout << endl ;
	for (int i = 0 ; i < arrSize ; i++) { 
		cout << row2[i] ;
	}
	cout << endl ;
	for (int i = 0 ; i < arrSize ; i++) { 
		cout << row3[i] ;
	}
	cout << endl ;
	for (int i = 0 ; i < arrSize ; i++) { 
		cout << row4[i] ;
	}
	cout << endl ;
	for (int i = 0 ; i < arrSize ; i++) { 
		cout << row5[i] ;
	}
	cout << endl ;
	 cout << "3 - Move Left" << endl ;
		cout << "4 - Move Right" << endl ;
		cout << "5 - Move Up" << endl ;
		cout << "6 - Move Down" << endl ;
		cout << "7 - Reset" << endl ;
		cout << "9 - Exit" << endl ;

		cout << endl << "Enter choice: " ;
}
//gets the player position and displays it
void addPlayer(int &row, int &col) { 
	reset() ;
	if (row < 0) row = arrSize -1 ;
	if (row >= arrSize) row = 0 ;
    if (col < 0) col = arrSize -1 ;
	if (col >= arrSize) col = 0 ;
	if (row == 0) row1[col] = '&' ;
	else if (row == 1) row2[col] = '&' ;
	else if (row == 2) row3[col] = '&' ;
	else if (row == 3) row4[col] = '&' ;
	else if (row == 4) row5[col] = '&' ;
}
int _tmain(int argc, _TCHAR* argv[])  {  
    int row = 0 ;
	int col = 0 ;
	addPlayer(row, col) ;
	int choice = 0 ;
	srand ( time(NULL) ) ;  
	do { disBoard() ;
	cin >> choice ;
	if (choice == 3) { 
			col-- ;
			addPlayer(row, col) ;
			disBoard() ;
		}
	else if (choice == 4) { 
			col++ ;
			addPlayer(row, col) ;
		}
	else if (choice == 5) { 
			row-- ;
			addPlayer(row, col) ;
		}
	else if (choice == 6) { 
			row++ ;
			addPlayer(row, col) ;
		}
	else if (choice == 7) { row=0 ;
			col = 0 ;
			addPlayer(row, col) ;
		}
	else if (choice == 9) { cout << "Thank you for  playing" ;}//else if  statement
    else (cout << "Please enter a valid number") ;//else statement
	system("cls") ;
	}
	while(choice != 9) ;//does the above again if  the statement given is true	
	
   system ("pause") ; 
   return 0 ; 
} 

Now he wants us to convert this to 2D arrays and I am unsure how I would do something like that. I have never used them. Please help.
I think he meant to change declaration

1
2
3
4
5
char row1[] =  { '-','-','-', '-','-'} ;
char row2[] =  { '-','-','-', '-','-'} ;
char row3[] =  { '-','-','-', '-','-'} ;
char row4[] =  { '-','-','-', '-','-'} ;
char row5[] =  { '-','-','-', '-','-'} ;


to

1
2
3
4
5
6
size_t N = 5;
char Board[N][N] = { { '-','-','-', '-','-'},
                     { '-','-','-', '-','-'},
                     { '-','-','-', '-','-'},
                     { '-','-','-', '-','-'}.
                     { '-','-','-', '-','-'} };  

Last edited on
Topic archived. No new replies allowed.