Commit 1291831e authored by Nelson Phillips's avatar Nelson Phillips
Browse files

autosave

parent c3fed2f8
#include <iostream>
#include <vector>
#include <stdio.h>
#include <unistd.h>
using namespace std;
void Sleep(float s)
{
int sec = int(s*100000);
usleep(sec);
}
main(){
char grid[15][15];
int x = 0;
int y = 0;
int score = 0;
int h = (rand() % 14);
int o = (rand() % 14);
char letter = ' ';
while(1){
system("stty raw");
// Wait for single character
char input = getchar();
// THIS IS JUST SOMETHING I THREW TOGETHER IF YOU WANT TO TRY TO UNDERSTAND HOW TO CREATE A WINDOW FOR A GAME OR SCREEN
// Reset terminal to normal "cooked" mode
if(input == 'w'|| input == 'W')
x--;
else
if(input == 'a'|| input == 'A')
y--;
else
if(input == 's'|| input == 'S')
x++;
else
if(input == 'd'|| input == 'D')
y++;
else
x++;
system("stty cooked");
for (int p = 0; p < 40; p++){
cout << endl;
}
for (int p = 0; p < 40; p++){
cout << endl;
}
cout << "Your score is : " << score << endl;
for (int i = 0; i < 14; i++){
cout << endl;
for (int q = 0; q < 14; q++){
if (x < 0)x++;
if (y < 0)y++;
if (x > 13)x--;
if (y > 13)y--;
if (h == i || o == q)
grid[h][o] = '*';
if (i == x && q == y)
grid[x][y] = 'H';
else if (i != h || o != q)
grid[i][q] = letter;
cout << grid[i][q] << " ";
}
}
if (grid [x][y] == grid[h][o] ){
h = (rand() % 14);
o = (rand() % 14);
score++;
}
}
}
...@@ -3,36 +3,35 @@ ...@@ -3,36 +3,35 @@
#include <thread> // std::thread #include <thread> // std::thread
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include "threadFunc.cpp"
using namespace std; using namespace std;
char grid[15][15]; char grid[15][30];
int x = 0; int x = 0;
int y = 0; int y = 0;
int score = 0; int score = 0;
int h = (rand() % 14); int h = (rand() % 14);
int o = (rand() % 14); int o = (rand() % 30);
char letter = ' '; char letter = ' ';
ReadInput inputRead;
char input; char input;
void Sleep(float s) { void Sleep(float s) {
int sec = int(s*500000); int sec = int(s*250000);
usleep(sec); usleep(sec);
} }
void foo() { void charCall() {
for (int i = 0; i < 5000; i++){ for (int i = 0; i < 5000; i++){
system("stty raw"); input = inputRead.collectInput();
input = getchar();
system("stty cooked");
} }
} }
void bar(){ void snakePrint(){
for (int i = 0; i < 5000; i++){ for (int i = 0; i < 5000; i++){
Sleep(1); Sleep(1);
system("stty cooked"); system("stty cooked");
...@@ -59,11 +58,11 @@ void foo() { ...@@ -59,11 +58,11 @@ void foo() {
for (int i = 0; i < 14; i++){ for (int i = 0; i < 14; i++){
cout << endl; cout << endl;
for (int q = 0; q < 14; q++){ for (int q = 0; q < 30; q++){
if (x < 0)x++; if (x < 0)x++;
if (y < 0)y++; if (y < 0)y++;
if (x > 13)x--; if (x > 12)x--;
if (y > 13)y--; if (y > 29)y--;
if (h == i || o == q) if (h == i || o == q)
grid[h][o] = '*'; grid[h][o] = '*';
if (i == x && q == y) if (i == x && q == y)
...@@ -76,21 +75,19 @@ void foo() { ...@@ -76,21 +75,19 @@ void foo() {
} }
} }
if (grid [x][y] == grid[h][o] ){ if (grid [x][y] == grid[h][o] ){
h = (rand() % 14); h = (rand() % 13);
o = (rand() % 14); o = (rand() % 30);
score++; score++;
} }
system("stty raw");
system("stty raw");
} }
} }
int main() int main()
{ {
thread first (foo); // spawn new thread that calls foo() thread first (charCall); // spawn new thread that calls input()
thread second (bar); // spawn new thread that calls bar(0) thread second (snakePrint); // spawn new thread that calls snakePrint(0)
cout << "main, foo and bar now execute concurrently...\n"; cout << "main, input and snakePrint now execute concurrently...\n";
// synchronize threads: // synchronize threads:
first.join(); // pauses until first finishes first.join(); // pauses until first finishes
......
#ifndef THREADCLASS_H
#define THREADCLASS_H
#include <iostream>
class ReadInput {
public :
char collectInput();
private:
char inputCh;
};
#endif
#include <iostream>
#include <stdio.h>
#include "threadClass.h"
char ReadInput::collectInput (){
system("stty raw");
inputCh = getchar();
system("stty cooked");
return inputCh;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment