Hi, i am trying to make a program that writes in a square board(which is an array bool[][]) given the instructions saved in a string[]. Any ideas?
How could it be done? Please I need some help here.
Here is what I got, the board before the instructions.
1 2 3 4 5 6 7 8 9
int N=20;
bool board[N][N];
//All elements of board are false
for(int i=N;i>=0;i--){
for(int a=N;a>=0;a--){
board[i][a]=false;
}
std::cout<<std::endl;
}
So I need code that can read intruc(iteration) and relate it with board[][] so that if intruc[c]=="up" then boar[n][m]=board[n+1][m]. I tried this but it doesnt work.
#include<iostream>
#include<fstream>
int main(){
int N=20;
bool board[N][N];
//All elements of board are false
for(int row=N;row>=0;row--){
for(int column=N;column>=0;column--){
board[row][column]=false;
}
std::cout<<std::endl;
}
// Read the file and gives the number of commands.
std::ifstream w("in.txt");
int words = 0;
std::string word;
while(w >> word){
words++;
}
w.close();
std::cout<<"Number of comands: "<<words<<std::endl;
//Read the file and puts all the commands in the string instruc[]
std::ifstream F;
F.open("in.txt");
std::string instruc[words];
for(int n=0;n<=words;n++){
F >> instruc[n];
}
F.close();
//Shows all the commands in the screen
bool drawn;
for(int n=0;n<=words;n++){
std::cout<<instruc[n]<<"\t";
}
int row=21;
int column=21;
//Use the commands for the array.
//Don't work, this is my Problem.
for(int c=0;c<=words;c++){
if(instruc[c]=="left"){
row=row+1;
}
if(instruc[c]=="right"){
row=row-1;
}
if(instruc[c]=="down"){
column=column-1;
}
if(instruc[c]=="up"){
column=column+1;
}
if(instruc[c]=="true"){
drawn=true;
}
if(instruc[c]=="false"){
drawn=false;
}
}
//Shows the modified board
//The board is not how it should be
for(row=0;row<=41;row++){
for(column=0;column<=41;column++){
std::cout<<board[row][column]<<"\t";
}
std::cout<<"\n";
}
std::cout<<" The program has ended";
return 0;
}
You don't ever do anything to actually change the values stored in board. You initialise the values in lines 7 - 12, but you never then change them anywhere.