SnakeTest.cpp 1.3 KB
Newer Older
Nelson Phillips's avatar
Nelson Phillips committed
1 2 3 4 5
#include <iostream>
#include "ReadInput.h"
#include "Threads.h"
#include "Snake.h"
#include <thread>
Nelson Phillips's avatar
Nelson Phillips committed
6 7
#include <vector>

Nelson Phillips's avatar
Nelson Phillips committed
8 9 10 11 12 13 14 15

using namespace std;

Snake BrdSet;
Thread Sneko;
ReadInput inputRead;

void charCall() {
Nelson Phillips's avatar
Nelson Phillips committed
16
    while (!BrdSet.quit()){
Nelson Phillips's avatar
Nelson Phillips committed
17 18 19
        BrdSet.input = inputRead.collectInput();
        if (BrdSet.input == 'q' || BrdSet.input == 'Q')
            break;
Nelson Phillips's avatar
Nelson Phillips committed
20
            
Nelson Phillips's avatar
Nelson Phillips committed
21 22 23 24
    }
}

    void snakePrint(){
Nelson Phillips's avatar
Nelson Phillips committed
25
       while (!BrdSet.quit()){
Nelson Phillips's avatar
Nelson Phillips committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
            Sneko.Sleep(1);
            
            system("stty cooked");
            
            if (BrdSet.input == 'q' || BrdSet.input == 'Q')
                break;
                
            BrdSet.movement(BrdSet.input);
            
            BrdSet.clrScreen();
            
            cout << "Your score is : " << BrdSet.snekoScore() <<  endl;
            
            BrdSet.draw();
            system("stty raw");
            
        }
    }
int main() 
{
Nelson Phillips's avatar
Nelson Phillips committed
46
    BrdSet.newBody();
Nelson Phillips's avatar
Nelson Phillips committed
47
    
Nelson Phillips's avatar
Nelson Phillips committed
48 49 50 51 52 53 54 55
    thread first (charCall);     // spawn new thread that calls input()
    thread second (snakePrint);  // spawn new thread that calls snakePrint(0)
    cout << "Press w,a,s,d to begin.";

    // synchronize threads:
    first.join();                // pauses until first finishes
    second.join(); 
    return 0;
Nelson Phillips's avatar
Nelson Phillips committed
56 57 58 59 60
}


//g++ *.cpp -o main.out -pthread -std=c++11 && ./main.out
//echo *.cpp