2D Array HistoryTable storing and Updating Predictions.

I'm asked to implement this method:

You must use a 2D array to store the moves history table.

Definition of what moves are stored:
Enter 1 or 0, trying to outsmart the computer, which is going to attempt to forecast your guesses. On each move the score indicator moves to the left if the computer guesses correctly, and moves to the right if the computer does not.

Example of moves:
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
 
void DisplayBoard() {
	int start = 10;
	const int width = 19;
	char boardSize[width] = { " " };

	

	boardSize[start] = '^';

	cout << "x---------x+++++++++x" << endl;

	for (int i = 0; i < width; i++) {
		cout << boardSize[i];
	}
	cout << "\n";

	start = start - 1;
	start = start + 1;
	

		if (start == 5) 
			cout << "You're going to lose!" << endl;
		
		else (start == 15);
			cout << "You're winning!" << endl;
		
	}


The table must be updated each move (starting on the 4th move) to reflect what was chosen in each last-three-moves situation. [However there is no penalty if you use the general-case code for the first three moves.]

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
int main(){

int userInput;
int i,j;
int historyTable[8][2] = { {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0} };
	

*/
DisplayBoard();

	cout << turnCounter << Your input: ";
	cout << endl;
	
cin userInput;

turncounter++

//  i have not implemented fully yet, this table should only be displayed when pressing T
// i need some type of dynamic array, to store and later on compare my last 3 moves and predict the next move.
     if (lastThree == 000) && (nextMove = 0){  
     historyTable[1][1] = historyTable[1][1] = + 1;
}
        else if (lastThree == 000) && (nextMove == 1){
             historyTable[1][2] = historyTable[1][2] = + 1;
}


	for (i = 0 ; i < 8; i++)
	{
		for (j = 0; j < 2; j++) {   // made into 3 rather than 2
			
			cout << setw(2) << historyTable[i][j];
		}
		cout << endl;
	}

		
	return 0;
} 


the expected output of the table looks like this
-----0 1
--- ---
000 0 0
001 0 0
010 0 0
011 0 0
100 0 0
101 0 0
110 0 0
111 0 0

How can i print the binary converted numbers 1-8 on the left hand side of my array?

Second,
How can i create a second array, which is a dynamic array that stores all user inputs of either 1 or 0's to take the last 3 entered, update the table, and make a prediction of the next move if the prediction for the next move is based on the last 3 moves.

If the first three moves are 001, the computer would predict a random number 0 or 1 since there is no history of the this combination stored in the table. If the computer picks 0 and the players 4th move is 0, historyTable[2][1] will increment by 1 and computer will get a point. and if the historyTable[2][1] > historyTable[2][2] computer will predict a 0 for the 4th number next time the user enters 0 0 1, however if this time the user enters a 1, Increment historyTable[2][2] by +1, and so on for every possible combination.

What i described above is what ill be basing my predictions and output to the historyTable.
Last edited on
I can't understand this at all. You say "the history table" as if we're supposed to know what that is. What are these "moves" that you talk about? What is the "general-case code"? What do you mean by things like 000? That's just 0. And 011 is an octal literal with a value of 9, which is probably not what you mean. Even your "example" makes no sense since there is no element [8] in an array of 8 elements, only [0] to [7]. And you can't arbitrarily decide to loop up to 3 for the inner dimension if it's only 2.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
    using std::cout;

    int hist[8][2] = {{0}};

    for (int i = 0; i < 8; i++)
        cout << i/4 << i/2%2 << i%2   // print index in binary
             << "  " << hist[i][0]
             << "  " << hist[i][1] << '\n';
}

Last edited on
Sorry for the unclear original goal, All i was attempting to do was to display a binary count for my historyTable on the left hand side of the historyTable Array.
I now realize what i was asking to do with the array is not possible.
I updated the description and have an example of the implication i'm attempting to achieve with this code.

https://scratch.mit.edu/projects/3101732/

Hopefully this clears things up.

Thank you for the format to print index in binary, worked flawlessly.
1
2
3
4
5
6
7
8
9
for (i = 0 ; i < 8; i++)
	{
		cout << i / 4 << i / 2 % 2 << i % 2;
		for (j = 0; j < 2; j++) {
			
			cout << setw(2) << historyTable[i][j];
		}
		cout << endl;
	}


Output =
000 0 0
001 0 0
010 0 0
011 0 0
100 0 0
101 0 0
110 0 0
111 0 0


Last edited on
Topic archived. No new replies allowed.