Hi I am attempting to create a chess board to reintroduce myself to c++. I am having trouble with adding a member function to my array that the chess board is located in.
I believe I am structuring the problem incorrectly.
.cpp file:
#include "spaces.h"
#include <iostream>
char board::spaceLetter() {
return letter;
}
char board::spaceNumber() {
return number;
}
string board::getColor(board a) {
if (a.color() == true) //Also an error but not a big deal
return "black";
else
return "white";
}
void board::printBoard(board a[][8]) {
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
if (a[i][j].color() == true) { //This is where my problem is
cout << "w";
}
else
cout << "b";
}
cout << endl;
}
}
Header .h
#pragma once
#include <iostream>
using namespace std;
class board {
private:
int boardSpace[8][8];
bool color;
char number;
char letter;
public:
board(){
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
if (((i + j) % 2) == 0)
color = true; //black space
else
color = false;
}
}
}
char spaceLetter();
char spaceNumber();
string getColor(board);
void printBoard(board a[][8]);
};
Thank you!
Welcome to SO.
if (a.color() == true) //Also an error but not a big deal
color is not a function. It is a member variable. Remove the () from color().
Same mistake here:
if (a[i][j].color() == true)
Try running the code here and see if it works for you: https://rextester.com/GRG48268
Related
I managed to solve my problem, it working properly and giving the correct results. The problem now is that I have this warning: Implicit conversion from char* to bool[readability-implicit-bool-conversion].
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
bool is_letter(const char s) {
return ('a' <= s && s <= 'z') || ('A' <= s && s <= 'Z');
}
int main() {
const int MAX_LENGTH = 260;
const int VOWELS = 11;
char is_vowel[VOWELS] = "aeiouAEIOU", s[MAX_LENGTH];
ifstream fin("date.in");
int k;
cin >> k;
int start = -1,nrVowels = 0, finish = 0, counter = 0;
while (!fin.eof()) {
fin.getline(s, MAX_LENGTH);
int n = strlen(s);
int have_word = 0;
for (int i = 0; i < n; ++i) {
if (is_letter(s[i])) {
have_word = 1;
if (strchr(is_vowel, s[i])) {
++nrVowels;
}
if (counter == 0) {
start = i;
finish = i;
++counter;
} else {
finish = i;
}
} else if (have_word == 1) {
if (nrVowels >= k) {
for (int i = start; i <= finish; ++i) {
cout << s[i];
}
cout << "\n";
}
counter = 0;
have_word = 0;
nrVowels = 0;
}
}
if (have_word == 1) {
if (nrVowels >= k) {
for (int i = start; i <= finish; ++i) {
cout << s[i];
}
cout << "\n";
}
counter = 0;
nrVowels = 0;
finish = 0;
}
}
return 0;
}
The error appears on the line where I am searching for the vowels
"
if (strchr(is_vowel, s[i]))
"
strchr() returns a char *. You're then using it in a boolean operation. While it works, the compiler is suggesting you change the code to:
if (strchr(...) != nullptr)
Then there is no implicit conversion.
Note that there are people who think C++ implicit conversion should be removed. Jason Turner has a talk on this on YouTube. I have no idea how many bugs I've had over the years due to implicit conversion, which is why your compiler warns you about it.
I'm new to C++ and I'm trying to figure out why I get two "*" symbols in my game board when I'm moving around. The game is supposed to be about avoiding the troll (#). But I am getting duplicate # and * symbols, and I can't figure out why. It seems that the problem is either in one of the for loops or in the posX or posY variables, which I found out by commenting out segments of the code:
#include <iostream>
#include <string>
using namespace std;
void ClearScreen()
{
cout << string(100, '\n');
}
main()
{
int size_arrx = 10;
int size_arry = 20;
int posX = 0;
int posY = 0;
int trollX = size_arrx - 1;
int trollY = size_arry - 1;
char a[size_arry][size_arrx];
bool Alive = true;
char player = '*';
char troll = '#';
while (Alive == true) {
ClearScreen();
for (int i = 0; i<size_arrx; i++)
{
for (int j = 0; j<size_arry; j++)
{
a[i][j] = 'x';
}
}
for (int i = 0; i<size_arrx; i++)
{
for (int j = 0; j<size_arry; j++)
{
a[posX][posY] = player;
a[trollX][trollY] = troll;
cout << a[i][j];
if (posX< 0) {
a[posX = 0][posY] = player;
cout << a[i][j];
}
else if (posY< 0) {
a[posX][posY = 0] = player;
cout << a[i][j];
}
else if (posY > size_arry - 1) {
a[posX][posY = size_arry - 1] = player;
cout << a[i][j];
}
else if (posX > size_arrx - 1) {
a[posX = size_arrx - 1][posY] = player;
cout << a[i][j];
}
}
cout << endl;
}
char dir;
cin >> dir;
if (dir == 'w') {
trollX++;
posX--;
}
if (dir == 's') {
trollX--;
posX++;
}
if (dir == 'd') {
trollY--;
posY++;
}
if (dir == 'a') {
trollY++;
posY--;
}
}
if ((trollX == posX) && (trollY == posY)) {
Alive == false;
}
}
The result looks like this. I only want one *. The * can move perfectly fine, but a duplicate * follows the original * but 11 X's away.
xxxxxxxxxx*xxxxxxxxx <---- This is a duplicate *
*xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx#
xxxxxxxxx#xxxxxxxxxx <---- This is a duplicate #
Thanks in advance if you can help me
for (int i=0;i<size_arrx;i++){
for (int j=0;j<size_arry;j++){
a[i][j]='x';
}
}
a[posX][posY]=player;
a[trollX][trollY]=troll;
for (int i=0;i<size_arrx;i++){
for (int j=0;j<size_arry;j++){
cout << a[i][j];
Using this code gave the same error. I'm interpreting this as a[i][j]='x' populates all positions of a[][] with X's. a[posX][posY]=player; overwrites the position of the player with an * (could be x 2 y 5 for example) and then the board gets printed by cout << a[i][j];. I don't understand how a duplicate symbol gets thrown in there.
Let's simplify your program.
Initialize the board outside of the while loop.
There should be no reason to keep initializing it:
for (unsigned int row = 0; row < size_arry; ++row)
{
std::fill(&a[row][0], &a[row][size_arrx], 'x'); // Fill a row.
}
Printing the board should be simple:
for (unsigned int row = 0; row < size_arry; ++row)
{
for (unsigned int column = 0; column < size_arrx; ++column)
{
cout << a[row][column];
}
cout << '\n';
}
Now the character logic.
Every character has a position, row and column, of where it is. To ease restoration, every character should have a previous position also.
struct Position
{
unsigned int row;
unsigned int column;
};
Sorry about that code, the fingers and keyboard are not cooperating.
To move a character to a valid new position, you have to restore the previous position:
unsigned int player_now_x;
unsigned int player_now_y;
unsigned int player_prev_x;
unsigned int player_prev_y;
//...
a[player_prev_y][player_prev_x] = 'x';
a[player_now_y][player_now_y] = player;
For processing single letter commands, a switch statement may be more readable:
// Update previous position.
player_prev_x = player_now_x;
player_prev_y = player_now_y;
switch (dir)
{
case 'd':
if (player_now_y < size_arry)
{
++player_now_y;
}
break;
case 's':
if (player_now_x < size_arrx)
{
++player_now_x;
}
break;
// ...
}
Simplifications.
You can print the board with one cout if you add an extra column. The ending column of each row (except the last) will have a line ending character, '\n'. The last column of the last row will have a termination character, '\0'.
struct Board
{
void set_player(const Position& pos, char player_token)
{
a[pos.x][pos.y] = player_token;
}
void move_player(const Position& new_position,
const Position& previous_position,
char player_token)
{
set_player(previous_position, 'x');
set_player(new_position, player_token);
}
void print()
{
std::cout << &a[0][0] << "\n";
}
Board()
{
for (unsigned int y = 0; y < size_arry; ++y)
{
std::fill(&a[y][0], &a[y][size_arrx], 'x');
a[y][size_arrx - 1] = '\n';
}
a[size_arry - 1][size_arrx - 1] = '\0';
}
};
//...
Board b;
Position player_now;
Position player_prev;
const char player_token = '*';
//...
switch (dir)
{
case 'd':
if (player_now.y < size_arry)
{
++player_now.y;
}
//...
}
b.move_player(player_now, player_previous, player_token);
Sorry again, for the above code fragment, it's the fingers typing out what they want.
So I am trying to create this program to check the sort of a list of words to see whether they are in ascending, or descending order. I am copying the words from a file to an array of strings. I am told the regular comparison operators function the same with strings as they do with ints. However, when I run the program, it always outputs that the list is unordered (even when it is). I would greatly appreciate any help one could offer me. Thank you!
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int checkArraySort(int array_max, string arr[]);
int main(void)
{
const int array_max = 20;
string arr[array_max];
int d;
ifstream myfile_in;
myfile_in.open ("words_in.txt");
string line;
for(int i = 0; i < array_max; i++)
{
getline(myfile_in, line);
}
d = checkArraySort(array_max, arr);
if(d == -1)
{
cout << "The array is sorted in descending order!" << endl;
}
if(d == 0)
{
cout << "The array is not sorted!" << endl;
}
if(d == 1)
{
cout << "The array is sorted in ascending order!" << endl;
}
myfile_in.close();
return 0;
}
int checkArraySort(int array_max, string arr[])
{
bool y = false;
int j = 0;
for(int i = 0; i < array_max; i++)
{
if(arr[i] < arr[i-1])
{
j++;
}
if(j == (array_max))
{
y = true;
return -1;
}
}
j = 0;
for(int i = 0; i < array_max; i++)
{
if(arr[i] > arr[i-1])
{
j++;
}
if(j == (array_max))
{
y = true;
return 1;
}
}
if(y = false)
{
return 0;
}
}
if(y = false)
should be
if(y == false)
I have this program that graphs simple parametric equations on a board of a defined length and width by me. It compiles fine but prints multiple instances of the function in different positions of the graph. If someone could please help me figure out why I am getting this output, I would greatly appreciate it. I included comments throughout the code to help understand what is going on.
I do not have enough reputation to post a picture of the output but if you compile and execute it you will see what I am talking about.
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[M][N]) {
int i, j;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++)
if (i==0) cout << '=';
else if (j==0) cout << '|';
else if (i==N) cout << '=';
else if (j==M) cout << '|';
else if (p[i][j]== '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t,2)/60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[M][N], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[M][N];
print_board(p);
int t = 0;
while(t <= M) {
cout << string(80, '\n');
next_board(p , t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}
Please help and thank you for all who try!
everywhere in your program where you have
char p[M][N]
change them to
char p[N][M]
and you should get the results that youd expect, your mixing axes in your program
heres the whole working code if youd like
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
#include <string>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[N][M]) {
int i, j;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++)
if (i == 0) cout << '=';
else if (j == 0) cout << '|';
else if (i == N) cout << '=';
else if (j == M) cout << '|';
else if (p[i][j] == '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t, 2) / 60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[N][M], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[N][M];
print_board(p);
int t = 0;
while (t <= M) {
cout << string(80, '\n');
next_board(p, t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}
Problem is that I want 'o' , not to repeat... I tried to find but I failed!
Also explain me that how this happens. I am a new learning programmer!
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
using namespace std;
int main()
{
//all in-game variables here
string o = "o";
int oH = 0;
int oW = 0;
//variables ending point...
bool run = true;
bool frameShow = true;
char input;
int width = 53;
int height = 22;
string px[width][height];
while (run)
{
for (int xEmp=0; xEmp<height; xEmp++){
for (int yEmp=0; yEmp<width; yEmp++)
{
px[xEmp][yEmp]= " ";
}
if (frameShow)
{
clrscr(); // Must be at start
px[oH][oW] = o;
for (int x=0; x<height; x++)
{
for (int y=0; y<width; y++)
{
cout << px[x][y];
}
cout << "\n";
frameShow = false; // Must be at end
}
}
if (kbhit())
{
input = getch();
// Most Used ones are:
// char 119 for "w"
// char 97 for "a"
// char 115 for "s"
// char 100 for "d"
// char 32 for "space"
// char 27 for ESC
if (input == 119)
{
oH--;
frameShow = true;
}
else if (input == 115)
{
oH++;
frameShow = true;
}
else if (input == 97)
{
oW--;
frameShow = true;
}
else if (input == 100)
{
oW++;
frameShow = true;
}
else if (input == 27)
{
// Game exits...
// To terminate, use:
run = false;
}
if(oH > height)
{
oH = height;
}
else if(oH < 0)
{
oH = 0;
}
if(oW > width - 1)
{
oW = width - 1;
}
else if(oW < 0)
{
oW = 0;
}
}
}
}
// Output for confiming program termination
clrscr();
cout << "\n - Terminated! - \n";
return 0;
}
As a start, width and height use is being changed. px[width][height];, px[xEmp][yEmp], xEmp<height, yEmp<width. One time you use it as px[width][height];, then px[height][width];. keep your code coherent!
Also if(oH > height) { oH = height; } is wrong. subtract one from height.
This also probably doesn't do what you want:
for (int x=0; x<height; x++)
for (int y=0; y<width; y++)
{
cout << px[x][y];
}
cout << "\n";
Use brackets correctly, IF you don't know how to use them, put them ALWAYS!
Again, I think you're not using brackets correctly:
for (int xEmp=0; xEmp<height; xEmp++){
for (int yEmp=0; yEmp<width; yEmp++)
{
px[xEmp][yEmp]= " ";
}
... // do other things
}
I think you want to close it right away to set everything back to space and them do other works. As it is now, it will set one line back to spaces, print everything and run your code, and only after empty next line.
for (int xEmp=0; xEmp<height; xEmp++){
for (int yEmp=0; yEmp<width; yEmp++)
{
px[xEmp][yEmp]= " ";
}
}
... // do other things
Ps: clrscr() is a non-standard function, only works in Windows, I think, for Linux use system('clr');
Ps2: Why are you using std::string instead of char if you only store chars?