- #ifndef GAME_H
- #define GAME_H
- #include "Square.h"
- #include <iostream>
- #include "Square.h"
- #include "Button.h"
- class Game {
- Square **r;
- // Rectangle r[5];
- int count;
- bool gameStarted;
- bool tie;
- bool horizontal;
- bool vertical;
- bool diagonal1;
- bool diagonal2;
- Button button3;
- Button button4;
- Button button5;
- Button buttonTie;
- Button player;
- Button computer;
- Button play;
- Button player1Wins;
- Button player2Wins;
- Button playAgain;
- Button quit;
- // Square board[9];
- bool playerX;
- bool AI;
- bool end;
- void AIMove() {
- if (!playerX && !end) {
- for (int i = 0; i < count; i++) {
- for (int j = 0; j < count; j++) {
- if (r[i][j].isEmpty()) {
- r[i][j].playO();
- playerX = !playerX;
- return;
- }
- }
- }
- }
- }
- void init(){
- r = new Square*[count];
- for (int i = 0; i < count; i++){
- r[i] = new Square[count];
- }
- float x = -0.9;
- float y = 0.9;
- float size = 1.8 / count;
- float w = 1.8 / count;
- float h = 1.5 / count;
- // Initialize your state variables
- for (int i = 0; i < count; i++){
- x = -0.9;
- for (int j = 0; j < count; j++){
- r[i][j] = Square(x, y, size);
- x += size;
- }
- y -= size;
- }
- }
- public:
- Game() {
- button3 = Button("3 x 3", -0.8, 0.0);
- button4 = Button("4 x 4", -0.2, 0.0);
- button5 = Button("5 x 5", 0.4, 0.0);
- player = Button("Human vs Human", -0.8, 0.3);
- computer = Button("Human vs AI", 0.2, 0.3);
- play = Button("Start", -0.2, -0.3);
- player1Wins = Button("Player O wins", -0.5, 0.3);
- player2Wins = Button("Player X wins", -0.5, 0.3);
- playAgain = Button("Play Again", -0.8, -0.3);
- quit = Button("Quit", 0.2, -0.3);
- buttonTie = Button("Game was a Draw", -0.5, 0.3);
- count = 3;
- init();
- // board[0] = Square(-0.9f, 0.9f, 0.6f);
- // board[1] = Square(-0.3f, 0.9f, 0.6f);
- // board[2] = Square(0.3f, 0.9f, 0.6f);
- // board[3] = Square(-0.9f, 0.3f, 0.6f);
- // board[4] = Square(-0.3f, 0.3f, 0.6f);
- // board[5] = Square(0.3f, 0.3f, 0.6f);
- // board[6] = Square(-0.9f, -0.3f, 0.6f);
- // board[7] = Square(-0.3f, -0.3f, 0.6f);
- // board[8] = Square(0.3f, -0.3f, 0.6f);
- playerX = true;
- AI = false;
- gameStarted = false;
- end = false;
- tie = false;
- }
- void checkWin(){
- for(int i = 0; i < count; i++){
- if(r[i][0].getPlayer() != EMPTY){
- bool win = true;
- for(int j = 1; j < count; j++){
- if(r[i][j].getPlayer() != r[i][j - 1].getPlayer()){
- win = false;
- break;
- }
- }
- if(win){
- end = true;
- return;
- }
- }
- }
- for(int j = 0; j < count; j++){
- if(r[0][j].getPlayer() != EMPTY){
- bool win = true;
- for(int i = 1; i < count; i++){
- if(r[i][j].getPlayer() != r[i - 1][j].getPlayer()){
- win = false;
- break;
- }
- }
- if(win){
- end = true;
- return;
- }
- }
- }
- if(r[0][0].getPlayer() != EMPTY){
- bool win = true;
- for(int i = 1; i < count; i++){
- if(r[i][i].getPlayer() != r[0][0].getPlayer()){
- win = false;
- break;
- }
- }
- if(win){
- end = true;
- return;
- }
- }
- if(r[0][count - 1].getPlayer() != EMPTY){
- bool win = true;
- for(int i = 1; i < count; i++){
- if(r[i][count - 1 - i].getPlayer() != r[0][count - 1].getPlayer()){
- win = false;
- break;
- }
- }
- if(win){
- end = true;
- return;
- }
- }
- tie = true;
- for(int i = 0; i < count; i++){
- for(int j = 0; j < count; j++){
- if(r[i][j].isEmpty()){
- tie = false;
- break;
- }
- }
- if(!tie){
- break;
- }
- }
- if(tie){
- end = true;
- return;
- }
- }
- void AIOn() {
- AI = true;
- AIMove();
- }
- void AIOff() {
- AI = false;
- }
- void playerXFirst() {
- playerX = true;
- }
- void playerOFirst() {
- playerX = false;
- }
- void start() {
- if (AI) {
- AIMove();
- }
- }
- // void leftMouseDown( float x, float y ){
- // // Respond to left mouse down events
- // if (button3.contains(x, y)){
- // std::cout << "Will change to 3x3" << std::endl;
- // for (int i = 0; i < count; i++){
- // delete[] r[i];
- // }
- // delete[] r;
- // count = 3;
- // init();
- // }
- // else if (button4.contains(x, y)){
- // std::cout << "Will change to 4x4" << std::endl;
- // for (int i = 0; i < count; i++){
- // delete[] r[i];
- // }
- // delete[] r;
- // count = 4;
- // init();
- // }
- // else if (button5.contains(x, y)){
- // std::cout << "Will change to 5x5" << std::endl;
- // for (int i = 0; i < count; i++){
- // delete[] r[i];
- // }
- // delete[] r;
- // count = 5;
- // init();
- // }
- // else if(play.contains(x, y)){
- // std::cout << "Game Started" << std::endl;
- // gameStarted = true;
- // }
- // }
- void handleMouseClick(float x, float y) {
- if(gameStarted){
- for (int i = 0; i < count; i++) {
- for (int j = 0; j < count; j++) {
- if (r[i][j].contains(x, y)) {
- if (r[i][j].isEmpty() && !end) {
- if (playerX) {
- r[i][j].playX();
- } else {
- r[i][j].playO();
- }
- playerX = !playerX;
- checkWin();
- break;
- }
- }
- }
- }
- if (AI) {
- AIMove();
- checkWin();
- if(AI && end){
- if (playAgain.contains(x, y)){
- // init();
- std::cout << "New Game" << std::endl;
- gameStarted = false;
- end = false;
- }
- else if(quit.contains(x, y)){
- std::cout << "Game Over" << std::endl;
- //referenced from ChatGPT
- exit(0);
- }
- }
- // if(end && !gameStarted){
- // if (playAgain.contains(x, y)){
- // init();
- // std::cout << "New Game" << std::endl;
- // gameStarted = false;
- // end = false;
- // AIOff();
- // }
- // else if(quit.contains(x, y)){
- // std::cout << "Game Over" << std::endl;
- // //referenced from ChatGPT
- // exit(0);
- // }
- // }
- }
- else if(end){
- if (playAgain.contains(x, y)){
- // init();
- std::cout << "New Game" << std::endl;
- gameStarted = false;
- end = false;
- }
- else if(quit.contains(x, y)){
- std::cout << "Game Over" << std::endl;
- //referenced from ChatGPT
- exit(0);
- }
- }
- }
- else if(computer.contains(x,y)){
- std::cout << "Human vs AI" << std::endl;
- AIOn();
- }
- else if(player.contains(x, y)){
- std::cout << "Human vs Human" << std::endl;
- AIOff();
- }
- else if (button3.contains(x, y)){
- std::cout << "Will change to 3x3" << std::endl;
- for (int i = 0; i < count; i++){
- delete[] r[i];
- }
- delete[] r;
- count = 3;
- init();
- }
- else if (button4.contains(x, y)){
- std::cout << "Will change to 4x4" << std::endl;
- for (int i = 0; i < count; i++){
- delete[] r[i];
- }
- delete[] r;
- count = 4;
- init();
- }
- else if (button5.contains(x, y)){
- std::cout << "Will change to 5x5" << std::endl;
- for (int i = 0; i < count; i++){
- delete[] r[i];
- }
- delete[] r;
- count = 5;
- init();
- }
- else if(play.contains(x, y)){
- std::cout << "Game Started" << std::endl;
- gameStarted = true;
- }
- }
- void draw() {
- if(!gameStarted && !end){
- button3.draw();
- button4.draw();
- button5.draw();
- player.draw();
- computer.draw();
- play.draw();
- }
- else if(gameStarted && !end){
- for (int i = 0; i < count; i++){
- for(int j = 0; j < count; j++){
- r[i][j].draw();
- }
- }
- }
- else if(end){
- if(tie){
- buttonTie.draw();
- }
- else if(playerX){
- player1Wins.draw();
- }
- else if(!playerX){
- player2Wins.draw();
- }
- std::cout << "Game ended" << std::endl;
- playAgain.draw();
- quit.draw();
- }
- // if(gameStarted){
- // for (int i = 0; i < 9; i++) {
- // r[i]->draw();
- // }
- // }
- }
- };
- #endif
[text] game
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- game
- Paste Code
- 20 Apr-2024
- 11.18 Kb
You can Share it:
Latest Code Pastes