Clearing the terminal screen - c++

#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.

Related

Recursive function throws unhandled exception at 0x79B20AD2 (ucrtbased.dll)

I write minesweeper and the current task is to write a function that would uncover the areas that have no mines neighbouring to them. In the original minesweeper if you click within the area with no mines, it would open up an area until there are mines alongside its borders. For that I wrote the function unravel(). Here is the code:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <string>
using namespace std;
#define Str1D vector<string>
#define Str2D vector<Str1D>
#define Int1D vector<int>
#define Int2D vector<Int1D>
void unravel(Str2D &fogofwar, Int2D &display, int x, int y) {
for (int minusrows = -1; minusrows < 2; minusrows++){ // going through the
// neighbouring cells (+ the cell itself)
for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
if (x + minusrows > 0 && y + minuscolumns > 0 && x + minusrows < fogofwar.size() && y + minuscolumns < fogofwar[0].size()){ // checking
// if within borders
if (x > 0 && y > 0 && x < fogofwar.size() && y < fogofwar[0].size()) { // checking if the oririginal
// values are within borders
fogofwar[x + minusrows][y + minuscolumns] = to_string(display[x + minusrows][y + minuscolumns]); // revealing the
// neighbouring cells
if (display[x + minusrows][y + minuscolumns] == 0) { // if the cell is 0 on the display,
// open it and the 8 neighbouring to it cells
if (not (minusrows == 0 && minuscolumns == 0)) { // if it's not the same cell, of course,
// otherwise it's an endless cycle
unravel(fogofwar, display, x + minusrows, y + minuscolumns);
}
}
}
}
}
}
}
int main() {
int row, column, prob;
bool running = true;
cout << "Input width and height: ";
cin >> row >> column;
cout << endl << "Input mines probability (%): ";
cin >> prob;
cout << endl;
srand (time(NULL));
Int2D field(row + 1, Int1D(column + 1));
Int2D display(row + 1, Int1D(column + 1));
Str2D fogofwar(row + 1, Str1D(column + 1, "*"));
field[0][0] = 0; // field of mines
display[0][0] = 0; // display of neighbouring mines
fogofwar[0][0] = to_string(0); // what the player will see
for (int i = 1; i < row + 1; i++) { //assigning coordinates
field[i][0] = i;
display[i][0] = i;
fogofwar[i][0] = to_string(i);
}
for (int j = 1; j < column + 1; j++) { //assigning coordinates
field[0][j] = j;
display[0][j] = j;
fogofwar[0][j] = to_string(j);
}
for (int i = 1; i < row + 1; i++){ // filling the field with mines
for (int j = 1; j < column + 1; j++){
int x = rand() % 100;
if (x < prob) {
field[i][j] = 1;
}
else{
field[i][j] = 0;
}
}
}
cout << endl << endl;
for (int i = 0; i < row + 1; i++){ // printing field
for (int j = 0; j < column + 1; j++){
cout << " " << field[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
for (int i = 0; i < row + 1; i++){ // assigning the display of amount of neighbouring mines
for (int j = 0; j < column + 1; j++){
int count = 0;
if (i > 0 && j > 0){
for (int minusrows = -1; minusrows < 2; minusrows++){
for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
if (i + minusrows > 0 && i + minusrows < row + 1 && j + minuscolumns > 0 && j + minuscolumns < column + 1){
if (field[i + minusrows][j + minuscolumns] == 1){
count++;
}
}
}
}
display[i][j] = count;
}
cout << " " << display[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
while (running) {
for (int i = 0; i < row + 1; i++){
for (int j = 0; j < column + 1; j++){
cout << " " << fogofwar[i][j] << " ";
}
cout << endl;
}
cout << endl;
int x, y;
cout << endl << "Input the target cell (x, y): ";
cin >> x >> y;
cout << endl;
unravel(fogofwar, display, x, y);
}
return 0;
}
If I delete the recursivity by changing unravel(fogofwar, display, x + minusrows, y + minuscolumns); to continue; within the function unravel(), it works as intended. But I need to open up the entire area where there are 0's on the display. Any way to skirt the error or fix it for good?
First of all, I am unable to reproduce the error with the information in question. Please try to specify the complete usecase along with what values you are getting into error with.
However, there is an obvious problem in the implementation of unravel.
You go over the same cell multiple times, until the memory exceeds total memory of course (I believe this is the point your program crashes)
You should maintain the slots already visited. You can do this in multiple ways. I am providing one of the ways to handle this.
Try the following code:-
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <string>
using namespace std;
#define Str1D vector<string>
#define Str2D vector<Str1D>
#define Int1D vector<int>
#define Int2D vector<Int1D>
void unravel(Str2D &fogofwar, Int2D &display, int x, int y, vector<vector<bool> > &visited) {
for (int minusrows = -1; minusrows < 2; minusrows++){ // going through the
// neighbouring cells (+ the cell itself)
for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
if (x + minusrows > 0 && y + minuscolumns > 0 && x + minusrows < fogofwar.size() && y + minuscolumns < fogofwar[0].size()){ // checking
// if within borders
if (x > 0 && y > 0 && x < fogofwar.size() && y < fogofwar[0].size()) { // checking if the oririginal
// values are within borders
if (x > 0 && y > 0 && x < visited.size() && y < visited[0].size()) {
cout.flush();
}
visited[x][y] = true;
fogofwar[x + minusrows][y + minuscolumns] = to_string(display[x + minusrows][y + minuscolumns]); // revealing the
// neighbouring cells
if (display[x + minusrows][y + minuscolumns] == 0) { // if the cell is 0 on the display,
// open it and the 8 neighbouring to it cells
if (not visited[x + minusrows][y + minuscolumns]) { // if it's not the same cell, of course,
// otherwise it's an endless cycle
unravel(fogofwar, display, x + minusrows, y + minuscolumns, visited);
}
}
}
}
}
}
}
int main() {
int row, column, prob;
bool running = true;
cout << "Input width and height: ";
cin >> row >> column;
cout << endl << "Input mines probability (%): ";
cin >> prob;
cout << endl;
srand (time(NULL));
Int2D field(row + 1, Int1D(column + 1));
Int2D display(row + 1, Int1D(column + 1));
Str2D fogofwar(row + 1, Str1D(column + 1, "*"));
field[0][0] = 0; // field of mines
display[0][0] = 0; // display of neighbouring mines
fogofwar[0][0] = to_string(0); // what the player will see
for (int i = 1; i < row + 1; i++) { //assigning coordinates
field[i][0] = i;
display[i][0] = i;
fogofwar[i][0] = to_string(i);
}
for (int j = 1; j < column + 1; j++) { //assigning coordinates
field[0][j] = j;
display[0][j] = j;
fogofwar[0][j] = to_string(j);
}
for (int i = 1; i < row + 1; i++){ // filling the field with mines
for (int j = 1; j < column + 1; j++){
int x = rand() % 100;
if (x < prob) {
field[i][j] = 1;
}
else{
field[i][j] = 0;
}
}
}
cout << endl << endl;
for (int i = 0; i < row + 1; i++){ // printing field
for (int j = 0; j < column + 1; j++){
cout << " " << field[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
for (int i = 0; i < row + 1; i++){ // assigning the display of amount of neighbouring mines
for (int j = 0; j < column + 1; j++){
int count = 0;
if (i > 0 && j > 0){
for (int minusrows = -1; minusrows < 2; minusrows++){
for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
if (i + minusrows > 0 && i + minusrows < row + 1 && j + minuscolumns > 0 && j + minuscolumns < column + 1){
if (field[i + minusrows][j + minuscolumns] == 1){
count++;
}
}
}
}
display[i][j] = count;
}
cout << " " << display[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
while (running) {
for (int i = 0; i < row + 1; i++){
for (int j = 0; j < column + 1; j++){
cout << " " << fogofwar[i][j] << " ";
}
cout << endl;
}
cout << endl;
int x, y;
cout << endl << "Input the target cell (x, y): ";
cin >> x >> y;
cout << endl;
vector<vector<bool> > visited(row+1, vector<bool>(column+1, false));
unravel(fogofwar, display, x, y, visited);
}
return 0;
}
The change is that I am maintaining a visited array, and I never go back to the spot I have already gone to before in unravel.

Move a Character in a 2d Array

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.

Finding all saddle points in a matrix c++

I'm working on a code that finds all saddle points in a matrix. Both smallest in their row and biggest in their column, and biggest in their row and smallest in their column fall under the definition (of my university) of a saddle point. Being a beginner I managed to get half of it done (finding saddle points which are smallest in their row and biggest in their column) by copying parts of what we've done in class and typing it myself. I have been stuck on it for quite some time and can't figure how to add the saddle points which are biggest in their row and smallest in their column to the program.
This is what I have so far:
#include <iostream>
#include <cstdlib>
using namespace std;
int a[10][10];
int x, y;
int pos_max(int j) //saddle points check
{
int max = 0;
for (int i = 1; i <= x - 1; i++) {
if (a[i][j] > a[max][j]) {
max = i;
}
}
return max;
}
int main() {
cout << "Enter the number of rows: ";
cin >> x;
cout << "Enter the number of columns: ";
cin >> y;
cout << "----------------------------" << endl;
for (int i = 0; i <= x - 1; i++) //input of the matrix
for (int j = 0; j <= y - 1; j++) {
cout << "a[" << i + 1 << ", " << j + 1 << "] = ";
cin >> a[i][j];
}
cout << "----------------------------\n";
for (int i = 0; i <= x - 1; i++) //visualization of the matrix
{
for (int j = 0; j <= y - 1; j++)
cout << a[i][j] << " ";
cout << endl;
}
cout << "----------------------------\n";
int r;
int flag = 0;
int i = y;
for (int j = 0; j <= y - 1; j++) {
r = pos_max(j);
for (i = 0; i <= y - 1; i++) {
if (a[r][i] < a[r][j]) {
break;
}
}
if (i == y) {
cout << "Saddle points are: ";
cout << "a[" << r + 1 << ", " << j + 1 << "] = " << a[r][j] << "\n";
flag = 1;
}
}
if (flag == 0) {
cout << "No saddle points\n";
}
cout << "----------------------------\n";
return 0;
}
First, there is a logical error with your code. In the pos_max function, it will return the index of the element which is maximum in the column. There can be a case when there are multiple maximum with the same value in the column, however, it returns the one which is not the minimum in the row, hence your program won't be able to print that saddle point.
To solve this, you can either return an array of all indices which are maximum in a column and then check for each of those points if it's minimum in their respective column, but I think it's not a very elegant solution. In any case, you will again have to write the entire code for the other condition for saddle points, minimum in column and maximum in row.
Hence, I would suggest a change in strategy. You create 4 arrays, max_row, max_col, min_row, min_col, where each array stores the minimum / maximum in that row / column respectively. Then you can traverse the array and check if that point satisfies saddle point condition.
Here is the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int a[10][10];
int max_row[10], max_col[10], min_row[10], min_col[10];
int x, y;
bool is_saddle(int i, int j) {
int x = a[i][j];
return (max_row[i] == x && min_col[j] == x) || (min_row[i] == x && max_col[j] == x);
}
int main() {
/* code to input x, y and the matrix
...
*/
/* code to visualize the matrix
...
*/
/* populating max and min arrays */
for (int i = 0; i <= x-1; ++i) {
max_row[i] = a[i][0], min_row[i] = a[i][0];
for (int j = 0; j <= y-1; ++j) {
max_row[i] = max(max_row[i], a[i][j]);
min_row[i] = min(min_row[i], a[i][j]);
}
}
for (int j = 0; j <= y-1; ++j) {
max_col[j] = a[0][j], min_col[j] = a[0][j];
for (int i = 0; i <= x-1; ++i) {
max_col[j] = max(max_col[j], a[i][j]);
min_col[j] = min(min_col[j], a[i][j]);
}
}
/* Check for saddle point */
for (int i = 0; i <= x-1; ++i) {
for (int j = 0; j <= y-1; ++j) {
if (is_saddle(i, j)) {
cout << "Saddle points are: ";
cout << "a[" << i + 1 << ", " << j + 1 << "] = " << a[i][j] << "\n";
flag = 1;
}
}
}
if (flag == 0) {
cout << "No saddle points\n";
}
cout << "----------------------------\n";
return 0;
}
#include <iostream>
using namespace std;
int getMaxInRow(int[][5], int, int, int);
int getMinInColumn(int[][5], int, int, int);
void getSaddlePointCordinates(int [][5],int ,int );
void getInputOf2dArray(int a[][5], int, int);
int main()
{
int a[5][5] ;
int rows, columns;
cin >> rows >> columns;
getInputOf2dArray(a, 5, 5);
getSaddlePointCordinates(a,rows,columns);
}
void getInputOf2dArray(int a[][5], int rows, int columns)
{
for (int i = 0; i < rows; i = i + 1)
{
for (int j = 0; j < columns; j = j + 1)
{
cin >> a[i][j];
}
}
}
void getSaddlePointCordinates(int a[][5],int rows,int columns)
{
int flag = 0;
for (int rowNo = 0; rowNo < 5; rowNo++)
{
for (int columnNo = 0; columnNo < 5; columnNo++)
{
if (getMaxInRow(a, rows, columns, rowNo) == getMinInColumn(a, rows, columns, columnNo))
{
flag = 1;
cout << rowNo << columnNo;
}
}
}
if (flag == 0)
cout << "no saddle point";
cout << "\n";
}
int getMaxInRow(int a[][5], int row, int column, int rowNo)
{
int max = a[rowNo][0];
for (int i = 1; i < column; i = i + 1)
{
if (a[rowNo][i] > max)
max = a[rowNo][i];
}
return max;
}
int getMinInColumn(int a[][5], int row, int column, int columnNo)
{
int min = a[0][columnNo];
for (int i = 1; i < row; i = i + 1)
{
if (a[i][columnNo] < min)
min = a[i][columnNo];
}
return min;
}
just take the reference arr(ref[size]) // memorization method to check the minimum and maximum value in it.
Here is the Code Implementation with time complexity O(n *n) & space complexity O(n):
#include <bits/stdc++.h>
using namespace std;
#define size 5
void util(int arr[size][size], int *count)
{
int ref[size]; // array to hold all the max values of row's.
for(int r = 0; r < size; r++)
{
int max_row_val = arr[r][0];
for(int c = 1; c < size; c++)
{
if(max_row_val < arr[r][c])
max_row_val = arr[r][c];
}
ref[r] = max_row_val;
}
for(int c = 0; c < size; c++)
{
int min_col_val = arr[0][c];
for(int r = 1; r < size; r++) // min_val of the column
{
if(min_col_val > arr[r][c])
min_col_val = arr[r][c];
}
for(int r = 0; r < size; r++) // now search if the min_val of col and the ref[r] is same and the position is same, if both matches then print.
{
if(min_col_val == ref[r] && min_col_val == arr[r][c])
{
*count += 1;
if((*count) == 1)
cout << "The cordinate's are: \n";
cout << "(" << r << "," << c << ")" << endl;
}
}
}
}
// Driver function
int main()
{
int arr[size][size];
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
cin >> arr[i][j];
}
int count = 0;
util(arr, &count);
if(!count)
cout << "No saddle points" << endl;
}
// Test case -> Saddle Point
/*
Input1:
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
0 2 3 4 5
Output1:
The cordinate's are:
(0,4)
(2,4)
(4,4)
Input2:
1 2 3 4 5
6 7 8 9 1
10 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Output2:
No saddle points
*/

Connect Four C++

I am coding the game connect four for my computer science class, everything was going great then I dont know what happened but I kept getting Sementation faults whenever I tried to run it. Here is my code.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;
struct game{
char **board;
char p1;
char p2;
};
void print_board(game p, int r, int c);
bool play(game p, bool gamewon, int r, int c, int pieces);
bool check(int a, int b, game p, int r, int c, int pieces);
int drop(int b, char player, game p, int c);
int main(int argc, char *argv[]){
game p;
int r, c, pieces;
bool gamewon = false;
/*if(argc == 7) {
for(int i = 1; i < argc; i += 2) {
if(argv[i][0]=='-' && argv[i][1]=='r')
r = atoi(argv[i+1]);
else if(!strcmp(argv[i],"-c"))
c = atoi(argv[i+1]);
else if(!strcmp(argv[i],"-p")){
pieces = atoi(argv[i+1]);
while (pieces < 1){
cout << "You cannot have 0 pieces to connect." << endl;
cout << "Please enter a positive, non-zero integer"
<< " for the number of pieces to connect: ";
cin >> pieces;
}
}
else
cout << "Error." << endl;
}
}*/
r = 6;
c = 7;
pieces = 4;
p.board = new char*[r];
for(int i=0; i < r;i++)
p.board[i] = new char[c];
for(int a =0; a < r; a++){
for(int b = 0; b < c; b++)
p.board[a][b] = ' ';
}
print_board(p, r, c);
gamewon = play(p, gamewon, r, c, pieces);
return 0;
}
bool play(game p, bool gamewon, int r, int c, int pieces){
int col, hold = 0, charsPlaced = 0;
char player = 'y';
while(!gamewon){
if(hold != -1){
if(player =='y'){
cout<<"Player 1, what column do you want to put your piece? ";
player = 'r';
}
else{
cout<<"Player 2, what column do you want to put your piece? ";
player = 'y';
}
}
while(true){
if(charsPlaced == r*c) break;
cin>>col;
col--;
if(col <=r && col>= 0) break;
else cout<< "\nPlease enter a value between 1 and " << c << ": ";
if (cin.fail()){
cin.clear();
char d;
cin>>d;
}
}
if(charsPlaced == r*c) break;
hold = drop(col,player, p, c);
if(hold == -1) cout<<"Column is full!\nPlease enter another number between 1 and " << c << ": ";
else{
gamewon = check(hold, col, p, r, c, pieces);
charsPlaced ++;
print_board(p, r, c);
}
}
if(charsPlaced == r*c){
cout<<"No winner! Game is a draw\n";
return true;
}
if(player == 'y')
cout<<"Player 2 is the winner!\n";
else cout<<"Player 1 is the winner!\n";
return true;
}
void print_board(game p, int r, int c){
cout << endl;
for(int a = 0; a < r; a++){
for(int b = 0; b < c; b++)
cout << "|" << p.board[a][b];
cout << "|";
cout << endl;
for(int i = 0; i < c; i++)
cout << "--";
cout << endl;
}
}
bool check(int a, int b, game p, int r, int c, int pieces){
int vertical = 1, horizontal = 1, diagonalone = 1, diagonaltwo = 1, i , j;
cout << i << " " << b << " " << a << endl;
char player = p.board[a][b];
cout << player << endl;
for(i = a + 1; p.board[i][b] == player && i < r; i++, vertical++);
for(i = a - 1; p.board[i][b] == player && i >= 0; i--, vertical++);
if(vertical >= pieces)
return true;
for(j = b - 1; p.board[a][j] == player && j >= 0; j--, horizontal++);
for(j = b + 1; p.board[a][j] == player && j < c; j++, horizontal++);
if(horizontal >= pieces)
return true;
for(i = a - 1, j = b - 1; p.board[i][j] == player && i >= 0 && j >= 0; diagonalone++, i--, j--);
for(i = a + 1, j = b + 1; p.board[i][j] == player && i <= r && j <= c;diagonalone++, i++, j++);
if(diagonalone >= pieces)
return true;
for(i = a - 1, j = b + 1; p.board[i][j] == player && i >= 0 && j <= c; diagonaltwo++, i--, j++);
for(i = a + 1, j = b - 1; p.board[i][j] == player && i <= r && j >= 0; diagonaltwo++, i++, j--);
if(diagonaltwo >= pieces)
return true;
return false;
}
int drop(int b, char player, game p, int c){
if(b >= 0 && b <= c){
if(p.board[0][b] == ' '){
int i;
for(i = 0; p.board[i][b] == ' '; i++)
if(i == 5){
p.board[i][b] = player;
return i;
}
i--;
p.board[i][b] = player;
return i;
}
else{
return -1;
}
}
else{
return -1;
}
}
You are using
char **board;
in your game struct.
But this 2D array is never allocated and yet, you are working with it.
You are missing something like this:
in C:
board = malloc(10 * sizeof(char *));
for (int i = 0; i < 10; i++)
{
board[i] = malloc(10 * sizeof(char));
}
in C++:
board = new char*[10]
for (int i = 0; i < 10; i++)
{
board[i] = new char[10];
}
For starters, if you are in a Linux environment, valgrind is very helpful in finding where your segmentation fault is. To use it, if your program is called hello, just run it as valgrind hello.
Another method to debug these is to put cout or printf statements throughout your code, and observe what the last output was. You are probably indexing beyond the end of an array. Keep in mind that if you declare int x[5], the valid index values are 0 to 4.
You will probably need to change your for loops to < vs. <= so you aren't reading past the last part of the board (keep in mind that arrays are 0 indexed in c++).

Why is the axis for my program off

//============================================================================
// Name : Assignment.cpp
// Author : Tim Bialecki
// Version :
//============================================================================
#include <iostream>
#include <math.h>
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 = 75;
int b = 5;
int c = 4;
int d = 26;
/*cout << "please enter an x coordinate for the center of the circle";
cin >> x;
cout << "please enter a y coordinate for the center of the circle";
cin >> y;
cout << "please enter a value for the radius of the circle";
cin >> radius;*/
circle(a, b, c);
for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
return 0;
}
void circle(int x, int y, int radius){
/*if (x + radius >= 81 || y + radius >= 26 || y - radius <= 26){
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;
}
}
}
void line(int a, int b, int c, int d){
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 = 0; j < 81; j++) {
int newIntercept = i - (slope*j);
int test = abs (intercept - newIntercept);
if (test <= 0)
buffer[i][j] = true;
else
buffer[i][j] = false;
}
}
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;
}
}
}
This code is a work in progress, but I'm trying to write a program that takes inputs for the coordinates and dimensions of both a line and a circle and prints them out in the terminal window as if it were a 81x26 graph. I have just supplied sample inputs to test this out, but for some reason the shapes are not printing with the appropriate orientation to what should be the x and y axises. I have tried a bunch of different ways of trying to fix this problem and have had no luck. Hoping someone can help. Thanks
Looks OK to me:
***
** **
* *
* *
* *
* *
* *
** **
***
It's not perfectly round because characters are taller than they are wide.
EDIT: That's only the first few rows on my output. Based on the comment and a second look at the code, I think rows and columns are getting mixed up.
for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
There's a newline after every "column". Swapping the two for lines may produce what you want.