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;
}
}
Related
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.
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 am new to C++ programming and trying to solve some problems on UVa Online Judge our prof gave us.
Right now I am on the 706 LCD Display (https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=647).
In my IDE (VS2015 Community) everything works fine and as expected but when I'm trying to submit my code I get a runtime error. Unfortunately they don't show more details about it.
I tried to run my code in several online c++ compilers (such as http://cpp.sh/) and never expirienced an error.
This is my code
#include <iostream>
#include <cmath>
// set the "borders" for each number
#define ZERO 1+2+4+16+32+64;
#define ONE 4+32;
#define TWO 1+4+8+16+64
#define THREE 1+4+8+32+64
#define FOR 2+4+8+32
#define FIVE 1+2+8+32+64
#define SIX 1+2+8+16+32+64
#define SEVEN 1+4+32
#define EIGHT 1+2+4+8+16+32+64
#define NINE 1+2+4+8+32+64
using namespace std;
// draw the digits line by line. some lines (first, middle, last) only represents horizontal lines, where all other lines represent vertical lines
// in every line for every digit is checked wheter the lines on the current position must be drawn or not.
void drawDigits(int* digits, int cDigits, int height, int width) {
int middle = (height >> 1);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < cDigits; ++j) {
if (i == 0) {
if (digits[j] & 1) {
cout << " ";
for (int y = 0; y < width - 2; ++y) cout << "-";
cout << " ";
}
else {
for (int y = 0; y < width; ++y) cout << " ";
}
}
else if (i < middle) {
if (digits[j] & 2) {
cout << "|";
for (int y = 0; y < width - 2; ++y) cout << " ";
}
else {
for (int y = 0; y < width - 1; ++y) cout << " ";
}
if (digits[j] & 4) {
cout << "|";
}
else {
cout << " ";
}
}
else if (i == middle) {
if (digits[j] & 8) {
cout << " ";
for (int y = 0; y < width - 2; ++y) cout << "-";
cout << " ";
}
else {
for (int y = 0; y < width; ++y) cout << " ";
}
}
else if (i < height - 1) {
if (digits[j] & 16) {
cout << "|";
for (int y = 0; y < width - 2; ++y) cout << " ";
}
else {
for (int y = 0; y < width - 1; ++y) cout << " ";
}
if (digits[j] & 32) {
cout << "|";
}
else {
cout << " ";
}
}
else {
if (digits[j] & 64) {
cout << " ";
for (int y = 0; y < width - 2; ++y) cout << "-";
cout << " ";
}
else {
for (int y = 0; y < width; ++y) cout << " ";
}
}
if (j != cDigits - 1) cout << " ";
}
cout << endl;
if (i == height - 1) cout << endl;
}
}
// a given number (c-string) is prepared for drawing
// find out the length of the 'number' and assign for each digit the defined value in the ditigs-array
void drawLCD(int size, char* number) {
if (size < 1 || size > 10) return;
int length = 0;
while (*(number + length) != '\0') {
++length;
}
int* digits = new int[length];
for (int i = 0; i < length; ++i) {
int t = 0;
switch (*(number++) - '0') {
case 0:
t = ZERO;
break;
case 1:
t = ONE;
break;
case 2:
t = TWO;
break;
case 3:
t = THREE;
break;
case 4:
t = FOR;
break;
case 5:
t = FIVE;
break;
case 6:
t = SIX;
break;
case 7:
t = SEVEN;
break;
case 8:
t = EIGHT;
break;
case 9:
t = NINE;
break;
default:
t = ZERO;
}
digits[i] = t;
}
drawDigits(digits, length, 2 * size + 3, size + 2);
}
void callDraws(int cNums, int* size, char** numbers) {
for (int i = 0; i < cNums; ++i) {
drawLCD(size[i], numbers[i]);
}
}
// gets the integer value from a number in a c-string
int getInt(char* str) {
int length = 0;
int res = 0;
while (*(str + length) != '\0') {
++length;
}
for (int i = 0; i < length; ++i) {
res += (int)pow(10, length - (i + 1)) * (*(str++)-'0');
}
return res;
}
int main() {
char* n = new char[10];
int counter = 0;
int* size = new int[1000];
char** numbers = new char*[1000];
for (int i = 0; i < 1000; ++i) {
size[i] = 0;
numbers[i] = "";
}
while (cin >> n) {
if (size[counter] <= 0) {
int nn = getInt(n);
if (nn <= 0) break;
size[counter] = nn;
}
else {
numbers[counter++] = n;
n = new char[10];
}
}
callDraws(counter, size, numbers);
return 0;
}
When I enter a 'return 0' before the call to callDraws() the code terminates. So the error must be somewhere in printing the digits.
Where is this runtime error or how can I find it?
Thanks in advance.
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>
using namespace std;
void num7()
{
int numRows;
cin >> numRows;
for (int x = 0; x < numRows/2; x++) {
for (int y = 0; y <= x; y++) {
cout << "*";
}
cout << "\n";
}
float numRowsfloat = numRows;
double cos = numRowsfloat / 2;
int tan = numRowsfloat / 2;
double sin = tan;
if (cos == sin)
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
else
for (int x = 0; x < numRows/2+1; x++) {
for (int y = x; y >0; y--) {
cout << "*";
}
cout << "\n";
}
}
In the else column, it says expected expression.
This is trying to make a triangular shape. like
*
**
***
***
**
*
for inputed 6
or
*
**
***
**
*
for inputed 5
You problem is this:
if (cos == sin)
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
Here only the cout is part of the if statement. The loop is not. You need to add braces around the whole block.
You forgot the braces for the if statement. try this:
if (cos == sin) {
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
} else
for (int x = 0; x < numRows/2+1; x++) {
for (int y = x; y >0; y--) {
cout << "*";
}
cout << "\n";
}