I'm taking an intro to c++ class and I am quite stuck on a part to this project.
I need to have my character, 'H' move freely around the array. I have written a good amount of my code, but when I compile and run it, my hero isn't given the option to move. I don't know what is going wrong when I am calling my function in main. Any help would be gladly appreciated. I need his new position in the array to be maintained so that he can find the villain who is randomly placed in the array. I can work on the randint part later, but I am having a hard time simply getting 'H' to move.
Here is what I have so far:
Thank you.
#include <iostream>
using namespace std;
void printBoard(char board[][8])
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
cout << board[x][y];
}
cout << endl;
}
}
void move(char board[][8], char umove)
{
cout << "Please enter which direction you would like to move." << endl;
cin >> umove;
if (umove == 'x')
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
board[x][y] = x - 1;
}
}
}
else if (umove == 'd')
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
board[x][y] = y + 1;
}
}
}
else if (umove == 'a')
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
board[x][y] = y - 1;
}
}
}
else if (umove == 'w')
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
board[x][y] = x + 1;
}
}
}
}
char userinput()
{
char usermove;
cout << "Please enter the direction you want to go." << endl;
cin >> usermove;
return usermove;
}
int main()
{
char board[8][8];
int x;
int y;
while (true)
{
for (x = 0; x < 8; x++)
{
for (y = 0; y < 8; y++)
{
board[x][y] = 'e';
}
}
board[0][0] = 'H';
printBoard(board);
void move();
return 0;
}
}
you call void move() which is a method declaration and you must use move(...) instead for calling method. return 0 cause the app to finish which is not correct in this situation. you use infinite loop and you must use a condition for finish game.
depend on your description I suggest:
void printBoard(char board[][8]) {
// same as before
}
bool move(char board[][8], int &Hx, int &Hy) {
char umove;
cout << "Please enter which direction you would like to move." << endl;
cin >> umove;
if (umove == 'f') // f mean finish it
return false;
board[Hx][Hy] = 'e';
if (umove == 'a') // a mean left
Hy = Hy == 0 ? 7 : Hy - 1;
else if (umove == 'd') // d mean right
Hy = Hy == 7 ? 0 : Hy + 1;
else if (umove == 'w') // w mean up
Hx = Hx == 0 ? 7 : Hx - 1;
else if (umove == 's') // s mean down
Hx = Hx == 7 ? 0 : Hx + 1;
board[Hx][Hy] = 'H';
return true;
}
int main() {
char board[8][8];
int Hx = 0, Hy = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
board[x][y] = 'e';
}
}
board[Hx][Hy] = 'H';
bool res = true;
while (res) {
printBoard(board);
res = move(board, Hx, Hy);
}
cout << "Game finished!";
return 0;
}
You can make char board[][] and Hx and Hy (which contain current position of H) global and avoid sending them to method but this is not good at all.
I hope this is what you want.
Related
I used a two dimensional for loop to make a shape producing function for a class assignment but I couldn't get it to make a hollow square; only a filled one. I pass in two arguments the width and the symbol being used.
I've played around with it several times but I eventually just turned it in with one incompletion.
It should print something like this
++++
+ +
+ +
++++
#include <iostream>
#include "shapemaker.h"
using namespace std;
void shapemaker::Initialize(int w, int h, char sym)
{
width = w;
height = h;
symbol = sym;
}
int shapemaker::getcanWidth() {return width;}
int shapemaker::getcanHeight() {return height;}
char shapemaker::getSymbol() {return symbol;}
void shapemaker::setSymbol(char s) { symbol = s; }
void shapemaker::setcanWidth(int w) { width = w; }
void shapemaker::setcanHeight(int h) { height = h; }
void shapemaker::drawmidHorline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanHeight()/2;
for(int x = 0; x < drawingwidth; x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == midpoint)
cout << getSymbol();
}
cout << endl;
}
}
void shapemaker::drawmidVertline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanWidth()/2;
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingheight; y++)
{
if(y == midpoint)
{
cout << getSymbol();
}
cout << " ";
}
cout << endl;
}
}
void shapemaker::drawWidthsizedFullSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << symbol;
for(int y = 0; y < drawingwidth; y++)
{
cout << symbol;
}
cout << endl;
}
}
void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << getSymbol();
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1)
cout << getSymbol();
}
cout << getSymbol();
cout << endl;
}
}
If you want drawWidthsizedOpenSquare to print this
++++
+ +
+ +
++++
you should write this method like:
void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1 || y == 0 || y == drawingwidth-1) // border cell case
{
cout << getSymbol();
}
else
{
cout << " ";
}
}
cout << endl;
}
}
I am implementing Sudoku solver and using 2D vector and passing it around using reference but still, when at the end of the main I try to print the 2D vector it prints the initial 2D vector.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
void display(vector<vector<int>>& _board) {
for (auto row: _board) {
for (auto col: row) {
cout << col << " ";
}
cout << endl;
}
}
bool isBoardSolved(vector<vector<int>>& _board) {
for (auto row: _board) {
for (auto col: row) { //style[1] of 2D vector traversal
if (col == 0) {
return false;
}
}
}
return true;
}
map<int, int> findOptions(vector<vector<int>>& _board, int _row, int _col) {
map<int, int> options;
int x, y;
for (int digit = 1; digit < 10; ++digit) {
options[digit] = 0; //state 0 means available as options
}
//col in a row
for (y = 0; y < 9; ++y) {
if (_board[_row][y] != 0) {
options[_board[_row][y]] = 1;
}
}
//row in a col
for (x = 0; x < 9; ++x) {
if (_board[x][_col] != 0) {
options[_board[x][_col]] = 1;
}
}
//in a rectangular 3*3 matrix
if (_row <= 2)
x = 0;
else if (_row > 2 && _row <= 5)
x = 3;
else
x = 6;
if (_col <= 2)
y = 0;
else if (_col > 2 && _col <= 5)
y = 3;
else
y = 6;
for (int i = x; i < x + 3; ++i) {
for (int j = y; j < y + 3; ++j) {
if (_board[i][j] != 0) {
options[_board[i][j]] = 1;
}
}
}
return options;
}
void solveBoard(vector<vector<int>>& _board) {
int row = 0;
int col = 0;
bool flag = false;
if (isBoardSolved(_board)) {
// cout << "Solved Sudoku Board" << endl;
// display(_board); //gives correct answer when I print it here
return;
}
else {
for (int x = 0; x < 9; ++x) { //not using the style[1] because I need explicit index of empty slot
flag = false;
for (int y = 0; y < 9; ++y) {
if (_board[x][y] == 0) {
row = x;
col = y;
flag = true;
break;
}
}
if (flag)
break;
}
}
auto options = findOptions(_board, row, col);
for (auto digit: options) {
if (digit.second != 1) {
_board[row][col] = digit.first;
solveBoard(_board);
}
}
_board[row][col] = 0;
}
int main(int argc, char *argv[]) {
vector<vector<int>> board(9, vector<int>(9, 0));
board[0][3] = 3;
board[0][6] = 2;
board[2][1] = 7;
board[2][2] = 8;
board[2][3] = 0;
board[2][4] = 6;
board[2][6] = 3;
board[2][7] = 4;
board[3][1] = 4;
board[3][2] = 2;
board[3][3] = 5;
board[3][4] = 1;
board[4][0] = 1;
board[4][1] = 0;
board[4][2] = 6;
board[4][6] = 4;
board[4][7] = 0;
board[4][8] = 9;
board[5][4] = 8;
board[5][5] = 6;
board[5][6] = 1;
board[5][7] = 5;
board[6][1] = 3;
board[6][2] = 5;
board[6][4] = 9;
board[6][6] = 7;
board[6][7] = 6;
board[7][3] = 7;
board[8][2] = 9;
board[8][5] = 5;
cout << "Given Sudoku Board" << endl;
display(board);
solveBoard(board);
cout << "Solved Sudoku Board" << endl;
display(board); //gives unchanged answer when i print it here
}
What is wrong I am doing and how to correct it.
When I try this :
void change(vector<vector<int>>& _b){
_b[0][1] = 99;
}
int main(){
vector<vector<int>> b(1, vector<int>(9, 1));
cout<<b[0][1]<<endl;
change(b);
cout<<b[0][1];
return 0;
}
This displays the changed value for 2D vecor b.
You are not exiting the recursion call stack correctly in solveBoard(). Note the new bool function signature for solveBoard() being returned to signal up the invocation chain to exit early. Also note the three different return points now depending on where you are.
bool solveBoard(vector<vector<int>>& _board) {
int row = 0;
int col = 0;
bool flag;
if (isBoardSolved(_board)) {
// cout << "Solved Sudoku Board" << endl;
// display(_board); //gives correct answer when I print it here
return true;
}
else {
for (int x = 0; x < 9; ++x) { //not using the style[1] because I need explicit index of empty slot
flag = false;
for (int y = 0; y < 9; ++y) {
if (_board[x][y] == 0) {
row = x;
col = y;
flag = true;
break;
}
}
if (flag)
break;
}
}
auto options = findOptions(_board, row, col);
for (auto digit : options) {
if (digit.second != 1) {
_board[row][col] = digit.first;
if (solveBoard(_board))
return true;
}
}
_board[row][col] = 0;
return false;
}
I'm keeping track of a character's position on a game board through the use of a multidimensional array (board[10][20]). To allow for user movement, I have created a method, movePlayer(), that modifies the value of the index of where 'G' is located.
Whenever I do this, the character 'G' does move, but the previous location of 'G' remains on the gameboard, so there are two 'G's. My question is: How can I move an element (G) in a multidimensional array?
Main Function:
char userInput;
int main()
{
Game obj1;
cout << "New Game (y/n)" << endl;
cin >> userInput;
if(userInput == 'y')
{
obj1.gameBoard();
obj2.movePlayer();
}
}
Game(Class).cpp:
Game::Game()
{
for(int x = 0; x < 10 ; x++)
{
for(int y = 0; y < 20 ; y++)
{
board[x][y]= '.';
}
}
player = 'G';
treasure = 'X';
srand(time(0));
p_Pos1X = rand()%10;
p_Pos1Y = rand()%20;
t_Pos1X = rand()%10;
t_Pos1Y = rand()%20;
endSwitch = 0;
}
void Game::gameBoard()
{
printBoard(p_Pos1X,p_Pos1Y);
}
void Game::printBoard(int px, int py)
{
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 20 ; y++)
{
board[px][py] = player;
board[t_Pos1X][t_Pos1Y] = treasure;
cout << board[x][y] ;
}
cout << endl;
}
}
void Game:: movePlayer()
{
cin >> playerM;
switch(playerM)
{
case 'W':
case 'w':
movePlayerUp(p_Pos1X);
}
}
void Game::movePlayerUp(int m)
{
m = m - 1;
printBoard(m,p_Pos1Y);
}
If the objetive of the project is not more than a dots matrix and a G reaching a X you dont neds to store a matrix, of course following your approach the code below I hope to be the solution the change is in the printBoard function
Game::Game()
{
for(int x = 0; x < 10 ; x++)
{
for(int y = 0; y < 20 ; y++)
{
board[x][y]= '.';
}
}
player = 'G';
treasure = 'X';
srand(time(0));
p_Pos1X = rand()%10;
p_Pos1Y = rand()%20;
t_Pos1X = rand()%10;
t_Pos1Y = rand()%20;
endSwitch = 0;
}
void Game::gameBoard()
{
printBoard(p_Pos1X,p_Pos1Y);
}
void Game::printBoard(int px, int py)
{
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 20 ; y++)
{
if(x==px && y==py)
{
cout << player ;
}else if(x== t_Pos1X && y== t_Pos1Y ){
cout << treasure;
}else{
cout << board[x][y] ;
}
}
cout << endl;
}
}
void Game:: movePlayer()
{
cin >> playerM;
switch(playerM)
{
case 'W':
case 'w':
movePlayerUp(p_Pos1X);
}
}
void Game::movePlayerUp(int m)
{
m = m - 1;
printBoard(m,p_Pos1Y);
}
Why not just put a '.' in the player's position just before moving him to the new one?
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
void circle(int x, int y, int radius);
void line(int a, int b, int c, int d);
bool buffer[26][81];
char drawSpace[26][81];
int main() {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int x = 0;
int y = 0;
int radius = 0;
char choice;
cout << "Type 'c' to draw a circle or type 'l' to draw a line." << endl;
cin >> choice;
if (choice == 'c'){
cout << "please enter an x coordinate for the center of the circle \n";
cin >> x;
cout << "please enter a y coordinate for the center of the circle \n";
cin >> y;
cout << "please enter a value for the radius of the circle \n";
cin >> radius;
int moves = (x - radius) / 10;
for (int s = 0; s < moves; s++){
circle(x, y, radius);
system("clear");
x = x -10;
}
}
else if (choice == 'l'){
cout << "Please enter the x coordinate for the first point on the line \n";
cin >> a;
cout << "Please enter the y coordinate for the first point on the line \n";
cin >> b;
cout << "Please enter the x coordinate for the end point on the line \n";
cin >> c;
cout << "Please enter the y coordinate for the end point on the line \n";
cin >> d;
}
else
cout << "you did not enter an appropriate letter, please restart the program and try again."<< endl;
return 0;
}
void circle(int x, int y, int radius){
if (x + radius >= 81|| x - radius <= 0 || y + radius >= 26 || y - radius <= 0){
cout << "the coordinates provided for the circle will not fit on the screen" << endl;
return;
}
for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int a = abs (x - j);
int b = abs (y - i);
int distance = pow(a, 2) + pow(b, 2);
int realDistance = pow(radius, 2);
if (abs(realDistance - distance) <= 3){
buffer[i][j] = true;
}
}
}
for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n]){
drawSpace[m][n] = 42;
}
else
drawSpace[m][n] = 32;
}
}
for (int row = 25; row >= 0; row--) {
for (int col = 0; col < 81; col++) {
cout << drawSpace[row][col];
}
cout << "\n";
}
}
void line(int a, int b, int c, int d){
if (a >= 81 || c >= 81 || a <= 0 || c <= 0 || b >= 26 || d >= 26 || b <= 0 || d <= 0){
return;
}
int intercept = 0;
double rise = d - b;
double run = c - a;
double slope = rise/run;
intercept = b - (slope*a);
for (int i = 0; i < 26; i++) {
for(int j = 21; j < 81; j++) {
if (slope > 0){
if (j > a && j < c){
int newIntercept = i - (slope*j);
int test = abs (intercept - newIntercept);
if (test <= 0)
buffer[i][j] = true;
else
buffer[i][j] = false;
}
}
else if (slope < 0){
if (j < a && j > c){
int newIntercept = i - (slope*j);
int test = abs (newIntercept - intercept);
if (test <= 0)
buffer[i][j] = true;
}
else
break;
}
}
}
for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n])
drawSpace[m][n] = 42;
else
drawSpace[m][n] = 32;
}
}
for (int row = 25; row >= 0; row--) {
for (int col = 0; col < 81; col++) {
cout << drawSpace[row][col];
}
cout << "\n";
}
}
I have written this code for a programming assignment, the goal of which is to take inputs for the coordinates and dimensions of a circle or line, and to print them out to the terminal as if it were a graph. The second step is to get the shape to move from the right side of the screen to the left. I have started to write this code for the circle, however for some reason the system("clear") call does not seem to clear the screen, and it simply prints extra circles without getting rid of the older one. If someone could help I would really appreciate it.
Try:
cout << "\033[2J\033[1;1H";
Go http://en.wikipedia.org/wiki/ANSI_escape_code for more information.
On Linux (and other Unixes) you could also use the ncurses library to output to a terminal.
The original poster doesn't have enough rep yet, so I'm posting this here for him:
I was actually a bit off base. The system("clear") I was using actually did
work, the problem I was encountering was that I did not reset the bool
array I was using to plot out the points that needed to be drawn.
Thanks for the help, I learned a few things about how to clear the screen before I found my own problem.
I want this code to represent symbols and not numbers (A, O, X)?
Can someone give me a simple code to make the numbers into symbols? Thanks
int game[3][3];
int x, y;
int lines = 0;
// select a random grid
srand(time(0));
for(x = 0; x < 3; x++) {
for(y = 0; y < 3; y++) {
game[x][y] = rand() % 3;
cout << game[x][y];
if (y == 2)
cout << '\n';
}
}
for (y = 0; y < 2; y++)
if (game[0][y] == game[1][y] && game[0][y] == game[2][y])
lines++;
You can use a lookup table:
char convert_number_to_letter(unsigned number)
{
static const char characters[] = "AOX";
if (number >= sizeof(characters) - 1)
return '\0'; // or other error handling
return characters[number];
}
chat c;
switch(game[x][y])
{
case 0:
c = 'A';
break;
case 1:
c = 'O';
break;
case 2:
c = 'X';
break;
}
or
char c;
if(game[x][y] == 0)
c = 'A';
else if(game[x][y] == 1)
c = 'O';
else
c = 'X';
Use another array, filled with the desired character values, then index that array with the generated random number:
char chars[] = { 'A', 'O', 'X' };
...
for(x = 0; x < 3; x++) {
for(y = 0; y < 3; y++) {
game[x][y] = chars[rand() % 3];
...
}
}