Correcting a Tic Tac Toe-Draw function - c++

I am making a multiplayer tic tac toe game with a semi-graphical interface. I have made the code and most of it works.The only part of it that doesn't work is the draw function.
I do understand that I am using TurboC++,which is a highly out of date compiler,but the Indian education system follows only TurboC++,so I have to make my project in it.(The syllabus was changed to have Python instead of C++ recently but I happened to be in the last batch of students that will not be taught Python)
The problem is in the last part of the result() function. I was unable to find what was wrong with it.I have not used graphics.h because it is not in my syllabus.
result function alone:
struct mat //To store the positions and what is present in the 9 boxes
{
int x,y;char ch;
};
struct xo //To keep track of the match
{
int actp,actx,acty;
mat pos[3][3];
char flag;
void setup() //To create the boxes/bars
{
actp=1,actx=1,acty=1;
flag=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
pos[i][j].ch=0;
}
}
void result() //To find the result
{
char flage;
for(int i=0;i<3;i++) //Column win
{
if(pos[i][0].ch==pos[i][1].ch&&pos[i][1].ch==pos[i][2].ch)
flage=pos[i][0].ch;
}
for(i=0;i<3;i++) //Row win
{
if(pos[0][i].ch==pos[1][i].ch&&pos[1][i].ch==pos[2][i].ch)
flage=pos[0][i].ch;
}
if(pos[0][0].ch==pos[1][1].ch&&pos[1][1].ch==pos[2][2].ch) //Diagonal win
flage=pos[0][0].ch;
if(pos[0][2].ch==pos[1][1].ch&&pos[1][1].ch==pos[2][0].ch) //Other diagonal win
flage=pos[0][2].ch;
if(flage=='X')flag='1';
else if(flage=='O')flag='2';
else flag='0';
int chk=1;
for(i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(pos[i][j].ch=='0'){chk=0;gotoxy(3,15);cout<<chk;} }//Added cout statement for debugging
}
if(flag=='0'&&chk==0)flag='D';//I understand that the condition is supposed to be chk==1,but it works only if I have this condition
}
}a;
Here is the whole code,if necessary:
https://drive.google.com/open?id=19WMexp3Hw_p9hO3qiYm0HRj-GGAJeaTr
A screenshot:
https://i.stack.imgur.com/wGh7a.jpg
If I use the correct condition, the program says that the match is drawn just after 1 move.
With this wrong condition, the program works to a certain extent and is able to find winners but never declares a match drawn even if it happens.
Thanks for the help!!

I have corrected the errors, will elaborate soon. Thanks for the help.

Related

How can I 'pan out' (make screen bytes smaller) of the console screen to render 2d pyramids larger?

I am using Windows 10 with code blocks to compile my code.
I wonder WHY you want to do that. This seems to be a typical homework assignment problem for practising loops and logic. That usually is restricted to numbers easily fitting on a console. Could you elaborate what causes the need to go beyond say a size of 30? – Yunnosch 10 hours ago
#Yunnosch For pure hypothetical and experimental reasons. I essentially have a dream I could write a program that renders massive 2d pyramids on a console screen, and maybe create some game out of it. I was hoping I could figure out a way to 'pan out' of the console screen thus making the screen bytes smaller. Somebody has to know more than me, and can lead me in the right direction to accomplishing this '2d pyramid rendering' program. Thanks in advance! – Fibonacci 3 mins ago
My goal is to be able to render massive pyramid structures onto a console output screen. The code here simply asks for a number of rows and prints out a pyramid. ex. drawPyramid(50);
#include <iostream> // include iostream
using namespace std; // include std
// Draw pyramid function here
void drawPyramid(int rows)
{
// initializes space and creates for loop to keep track of rows, I and k
int space;
for(int i = 1, k = 0; i <= rows; ++i, k=0)
{
// draws spaces required
for(space = 1; space <= rows-i; ++space)
{
cout << " ";
}
// draws matter
while(k != 2*i-1)
{
cout << "* ";
++k;
}
//prints new line based on rows
cout << endl;
}
}
//driver program
int main()
{
bool fTrue = false;
// loops until user presses '0'
while(!fTrue)
{
cout << "Press (0) to quit...\n";
int i,rows;
cout << "Enter number of rows: \n";
cin >> rows;
drawPyramid(rows); // draw function called
if(rows == 0)
{
fTrue = true;
}
}
return 0;
}
Output:
While the program works great I'd like to be able to pass in bigger values such as drawPyramid(500); or drawPyramid(1000); anything above the value 50 produces results like so.
Results:
Hopefully you can understand what I am trying to ask. I want to be able to 'pan out' and move the asterisks closer together so that I can pass in larger input values into the drawPyramid() function... Thanks in advance!
I am using Windows 10 with code blocks to compile my code.

Why is my for loop running only 2 times instead of 10?

I am trying to find the third highest number in the array with O(n) space and time complexity.
MY program is obviously wrong but that is not the problem.
My for loop in the thirdhighestnum function seem to running only 2 times. I can't seem to find the reason for it. Can anyone help me. Sorry for such basic question I am a beginner here.
#include<iostream>
using namespace std;
int thirdhighestnum(int a[],int size)
{
cout<<" "<<size<<endl;
int first=a[0],second=0,third=0;
for(int i=1;i<size;i++)
{
cout<<a[i]<<";"<<endl;
if(a[i]>first)
{
first=a[i];
if(a[i+1]>first)
{
second=first;
first=a[i+1];
if(a[i+2]>first)
{ third=second;
second=first;
first=a[i+2];
}
}
cout<<i<<endl
return third;
}
}
}
int main()
{ int num,a[10];
cout<<"Enter the elements in the array"<<endl;
for(int i=0;i<10;i++)
cin>>a[i];
cout<<"Third highest number is "<<thirdhighestnum(a,10)<<endl;
return 0;
}
It's the location of your return third statement. The moment any number is larger than the first number, it exits the thirdhighestnum function and returns a value. Put it outside your for loop and it should be fine.
Actually it is executing only one time i.e. the first iteration only. When i=1 then it prints a[1] i.e. 2; after that it prints i i.e. 1 so it appears as 2;1 here you think it is printing 2 numbers.
The main problem is your return third statement which terminates your for loop in first iteration only.
This is known as nth_element and library function can do this for you.
As for why your code fails, there is a flaw in your logic
for(int i=1;i<size;i++)
{
// some code
if(a[i]>first)
{
// some more code
cout<<i<<endl
return third;
}
}
So your code exits the first time a[i] is greater than first.
[edit - After reading comments]
The generic name for this type of algorithm is a QuickSelect. It is unlikely you will find a faster algorithm, as this has been the subject of years of academic study.

Controls not responding properly in snake

I'm writing a basic snake game as console application in c++. It's based on two-dimensional array of "tile" structures. My problem is: when pressing button to change the direction the snake is going it doesn't work immidiately but waits for next "tick" instead. The function that manages game itself looks like this:
void board::play()
{
display();
while(1)
{
getInput();
delay(0.5);
getInput();
resetCursor();
tick();
display();
}
}
display() is pretty self-explanatory, displays whole board in console window.
delay() as well, it's function which gets number of seconds as a float and waits this much time before proceeding
getInput() looks like this:
void board::getInput()
{
int input;
if(kbhit())
{
input=getch();
if(input==KEY_LEFT)
turnLeft();
if(input==KEY_RIGHT)
turnRight();
if(input==KEY_UP)
turnUp();
if(input==KEY_DOWN)
turnDown();
}
}
resetCursor() sets the cursor to 0,0 coordinate so each time the board will write over itself and not one under another
And now for the game itself: class board contains amongst others field int direction. The tiles themselves contain a counter, which counts down by 1 with each move, and when reaches 0 the tile becomes empty. If the counter is equal to the lenght of a snake the tile is considered a head.
The tick()function does just that: decreases all counters by 1, remembers where head was and spawns a new head in the field next to previous one in direction specified. It looks like this:
void board::tick()
{
int posx, posy;
for(int i=0; i<boardSize; i++)
{
for(int j=0; j<boardSize; j++)
{
if(tab[i][j].getCounter()==lenght)
{
posx=i;
posy=j;
}
tab[i][j].tick();
}
}
switch(direction)
{
case UP: tab[posx-1][posy].addSnake(lenght);
break;
case DOWN: tab[posx+1][posy].addSnake(lenght);
break;
case LEFT: tab[posx][posy-1].addSnake(lenght);
break;
case RIGHT: tab[posx][posy+1].addSnake(lenght);
break;
}
}
Problem is, as stated before, that game waits one "tick" before changing direction when it should do so immediately after pressing associated button, e.g. when turning from UP to LEFT it does one more move up and only after that moves left.
It seems to me that you have an extra getInput() call. Could it be the problem?
while(1)
{
getInput(); // <--
delay(0.5);
getInput(); // <--
resetCursor();
tick();
display();
}
Solved.
Turns out there were too many things put into buffer, as the program started going crazy if you mashed controls randomly during waiting time. I solved it by replacing
if(kbhit())
{
input=getch();
//reactions
}
with
while(kbhit())
{
input=getch();
}
//reactions
It now checks every signal in the buffer and reacts only to the last one, so the delay is eliminated.

Text-Galaga: Continuous Text-Game Looping and Controlling Character

just for fun and possibly for a project for my computer science class, I was making a text based "Galaga" game. Your character is ">" and the enemies are "<" and the lasers are "~" or "-", point is, its pretty simple. 600-700 lines of code. Problem is, you have to enter something for the code to loop. I used system("stty raw") so that I don't have to press enter, but the game stops when you stop holding or pressing buttons. I want to make it a real continuous game that is just made out of text. Any way to accomplish this while also being able to control the movement of your character properly using WASD movements? I am also using Xcode on a macbook if that helps. And this is part of the main function just for some context:
int main()
{
cout<<" ██████╗ █████╗ ██╗ █████╗ ██████╗ █████╗ "<<endl;
cout<<"██╔════╝ ██╔══██╗██║ ██╔══██╗██╔════╝ ██╔══██╗"<<endl;
cout<<"██║ ███╗███████║██║ ███████║██║ ███╗███████║"<<endl;
cout<<"██║ ██║██╔══██║██║ ██╔══██║██║ ██║██╔══██║"<<endl;
cout<<"╚██████╔╝██║ ██║███████╗██║ ██║╚██████╔╝██║ ██║"<<endl;
cout<<" ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ "<<endl;
cout<<endl<<endl;
cout<<"Welcome to text-galaga!"<<endl;
cout<<"_________________________"<<endl;
cout<<"Use W to move forward,"<<endl;
cout<<"A to move left, S to move back,"<<endl;
cout<<"D to move right, and J to shoot"<<endl;
cout<<"_________________________"<<endl;
cout<<"Good luck!"<<endl<<endl;
cout<< "Enter any key to begin..."<<endl;
cin>>x;
// Set terminal to raw mode
system("stty raw");
char input ='c';
int height=10,wide=5;
int turns=0;
int num_destroyed=0;
// ACTUAL MAIN GAME OPERATIONS START HERE...
while((input!='Q'&& input!='q') && (num_destroyed<5) && dead==false)
{
// Wait for single character
input = getchar();
int i=0;
int j=0;
get_inputs(i, j, bad);
render(i, j, height, wide, input, destroyed, dead, bad, turns, shot,
constant_height, constant_wide, shot_distance, next, num_destroyed);
draw(height, wide, j, i, destroyed, dead, bad, shot_distance, shot,
constant_height, constant_wide);
if ((num_destroyed<5) && dead==false)
cout<<endl<<endl;
++turns;
}
// Reset terminal to normal "cooked" mode
system("stty cooked");
// Game End Information
if (num_destroyed==5)
{
cout<<"Congradulations! You win!"<<endl<<endl;
}
if (dead==true)
{
cout<<"You died. Game Over."<<endl<<endl;
}
cout<<"Frames used: "<<turns<<endl;
return 0;
// AND END HERE...
}
Anyways, I know that I should probably put "while (true)" and take out the "getchar()" once I have the rest figured out, but that alone doesn't seem to make it into a sufficient game like a text-galaga. Anyways, sorry about the long post. Thanks guys.

C++ program to compute lcm of numbers between 1 to 20 (project euler )

as the title explains this is a program to find lcm of numbers between 1 to 20. i found an algorithm to do this, here's the link
http://www.cut-the-knot.org/Curriculum/Arithmetic/LCM.shtml
there is a java applet on the webpage that might explain the algorithm better
Problem: i wrote the code compiler shows no error but when i run the code the program goes berserk, i guess may be some infinite loopig but i can't figure it out for the life of me. i use turbo c++ 4.5 so basically if anyone can look at the code and help me out it would be great . thanks in advance
Algorithm:
say we need to find lcm of 2,6,8
first we find the least of the series and add to it the number above it, i.e the series become
4,6,8
now we find the least value again and add to it the intitial value in the column i.e 2
6,6,8
so the next iteration becomes
8,6,8
8,12,8
10,12,8
10,12,16
12,12,16
14,12,16
14,18,16
16,18,16
18,18,16
18,18,24
20,18,24
20,24,24
22,24,24
24,24,24
as you can see at one point all numbers become equal which is our lcm
#include<iostream.h>
/*function to check if all the elements of an array are equal*/
int equl(int a[20], int n)
{
int i=0;
while(n==1&&i<20)
{
if (a[i]==a[i+1])
n=1;
else
n=0;
i++;
}
return n;
}
/*function to calculate lcm and return that value to main function*/
int lcm()
{
int i,k,j,check=1,a[20],b[20];
/*loading both arrays with numbers from 1 to 20*/
for(i=0;i<20;i++)
{
a[i]=i+1;
b[i]=i+1;
}
check= equl(a,1);
/*actual implementation of the algorith*/
while(check==0)
{
k=a[0]; /*looks for the least value in the array*/
for(i=0;i<20;i++)
{
if(a[i+1]<k)
{
k=a[i+1]; /*find the least value*/
j=i+1; /*mark the position in array */
}
else
continue;
}
a[j]=k+b[j]; /*adding the least value with its corresponding number*/
check= equl(a,1);
}
return (a[0]);
/*at this point all numbers in the array must be same thus any value gives us the lcm*/
}
void main()
{
int l;
l=lcm();
cout<<l;
}
In this line:
a[j]=k+b[j];
You use j but it is unitialized so it's some huge value and you are outside of the array bounds and thus you get a segmentation fault.
You also have some weird things going on in your code. void main() and you use cout without either saying std::cout or using namespace std; or something similar. An odd practice.
Also don't you think you should pass the arrays as arguments if you're going to make lcm() a function? That is int lcm(int a[], int b[]);.
You might look into using a debugger also and improving your coding practices. I found this error within 30 seconds of pasting your code into the compiler with the help of the debugger.
Your loop condition is:
while(n==1&&i<20)
So your equl function will never return 1 because if n happens to be 1 then the loop will just keep going and never return a 1.
However, your program still does not appear to return the correct result. You can split the piece of your code that finds the minimum element and replace it with this for cleanliness:
int least(int a[], int size){
int minPos = 0;
for(int i=0; i<size ;i++){
if (a[i] < a[minPos] ){
minPos = i;
}
}
return minPos;
}
Then you can call it by saying j = least(a, 20);. I will leave further work on your program to you. Consider calling your variables something meaningful instead of i,j,k,a,b.
Your equl function is using array indices from 0-20, but the arrays only have 1-19
j in lcm() is uninitialized if the first element is the smallest. It should be set to 0 at the top of the while loop
In the following code, when i=19, you are accessing a[20], which is out of the bounds of the array. Should be for(i=0;i<19;i++)
for(i=0;i<20;i++) {
if(a[i+1]<k)
You are not actually using the std namespace for the cout. this should be std::cout<<l
Your are including iostream.h. The standard is iostream without the .h, this may not work on such an old compiler tho
instead of hard-coding 20 everywhere, you should use a #define. This is not an error, just a style thing.
The following code does nothing. This is the default behavior
else
continue;