Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SimpleTicTacToe.cpp
- // SimpleTicTacToe.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "Game.h"
- int _tmain(int argc, _TCHAR* argv[])
- {
- Game::Start() ;
- return 0;
- }
- Game.h
- #pragma once
- #include "SFML\Window.hpp"
- #include "SFML\Graphics.hpp"
- #define SIZE 3
- class Game
- {
- public:
- static void Start();
- private:
- static int board[3][3] ;
- static bool IsExiting();
- static void GameLoop();
- static void LoadBackground() ;
- static void LoadX() ;
- static void LoadO() ;
- static void ShowSplashScreen();
- static void ShowMenu() ;
- static void ShowCredits() ;
- static void Play1P() ;
- static void Play2P() ;
- static bool checkWin() ;
- static void changeTurn() ;
- static void DrawGame() ;
- static void SetDraw () ;
- static void HandleClick (int x, int y) ;
- static void DoAI () ;
- static void ChooseTurn() ;
- enum GameState { Uninitialized, ShowingSplash, Paused,
- ShowingMenu, Playing1P, Playing2P, Credits, Exiting };
- enum {EMPTY, X, O, DRAW} ;
- static GameState _gameState;
- static sf::RenderWindow _mainWindow;
- static sf::Sprite _sprite ;
- static sf::Texture _texture ;
- static std::string _filename ;
- static bool _isLoaded ;
- static sf::RectangleShape borders[4] ;
- static sf::RectangleShape lastLine ;
- static sf::RectangleShape lastColumn ;
- static sf::RectangleShape sepLine ;
- static sf::Text Turn [4] ;
- static sf::Text Reset ;
- static sf::Text goMainMenu ;
- static int _win ;
- static int _turn ;
- static int _computer ;
- static int _player ;
- static sf::Sprite xSprite ;
- static sf::Sprite oSprite ;
- static sf::Sprite backSprite ;
- static sf::Texture xTexture ;
- static sf::Texture oTexture ;
- static sf::Texture backTexture ;
- };
- Game.cpp
- #include "stdafx.h"
- #include "Game.h"
- #include "SplashScreen.h"
- #include "MainMenu.h"
- #include "Credits.h"
- void Game::Start(void)
- {
- if(_gameState != Uninitialized)
- return;
- _mainWindow.create(sf::VideoMode(560,560,32),"Tic-Tac-Toe");
- _gameState = Game::ShowingSplash;
- SetDraw() ;
- ChooseTurn() ;
- std::srand(time(NULL)) ;
- while(!IsExiting())
- {
- GameLoop();
- }
- _mainWindow.close();
- }
- void Game::ChooseTurn()
- {
- _computer = std::rand() % 2 ;
- if (_computer == 0) _computer = Game::X ;
- else _computer = Game::O ;
- if (_computer == Game::X)
- _player = Game::O ;
- else _player = Game::X ;
- }
- bool Game::IsExiting()
- {
- if(_gameState == Game::Exiting)
- return true;
- else
- return false;
- }
- void Game::GameLoop()
- {
- switch (_gameState)
- {
- case Game::ShowingMenu:
- {
- ShowMenu() ;
- break ;
- }
- case Game::ShowingSplash:
- {
- ShowSplashScreen() ;
- break ;
- }
- case Game::Playing1P:
- {
- Play1P();
- break ;
- }
- case Game::Playing2P:
- {
- Play2P() ;
- break ;
- }
- case Game::Credits:
- {
- ShowCredits() ;
- break ;
- }
- }
- }
- void Game::ShowSplashScreen()
- {
- SplashScreen splashScreen ;
- splashScreen.Show(_mainWindow) ;
- _gameState = Game::ShowingMenu ;
- }
- void Game::ShowMenu()
- {
- MainMenu mainMenu ;
- MainMenu::MenuResult result = mainMenu.Show (_mainWindow) ;
- switch (result)
- {
- case MainMenu::Exit:
- _gameState = Game::Exiting ;
- break ;
- case MainMenu::Play1P:
- _gameState = Game::Playing1P ;
- break ;
- case MainMenu::Play2P:
- _gameState = Game::Playing2P ;
- break ;
- case MainMenu::Credits:
- _gameState = Game::Credits ;
- break ;
- }
- }
- void Game::ShowCredits()
- {
- Creditz credits ;
- credits.Show(_mainWindow) ;
- _gameState = Game::ShowingMenu ;
- }
- void Game::Play1P()
- {
- sf::Event Event ;
- if (_turn == _player)
- {
- while(_mainWindow.pollEvent (Event))
- {
- DrawGame() ;
- _mainWindow.display () ;
- if (Event.type == sf::Event::Closed ) _gameState = Game::Exiting ;
- if (Event.type == sf::Event::KeyPressed)
- {
- if (Event.key.code == sf::Keyboard::Escape) ShowMenu() ;
- }
- if (Event.type == sf::Event::MouseButtonPressed)
- {
- HandleClick (Event.mouseButton.x, Event.mouseButton.y) ;
- }
- }
- }
- else
- {
- DrawGame() ;
- DoAI() ;
- changeTurn() ;
- }
- }
- void Game::DoAI()
- {
- for (int i = 0 ;i < 3; i++)
- {
- for (int j = 0; j<3; j++)
- {
- if (board[i][j] == Game::EMPTY)
- {
- board[i][j] = _computer ;
- if (checkWin())
- {
- return ;
- }
- else board[i][j] = Game::EMPTY ;
- }
- }
- }
- for (int i = 0; i<3 ; i++)
- {
- for (int j = 0 ; j<3 ;j++)
- {
- if (board[i][j] == Game::EMPTY)
- {
- board[i][j] = _player ;
- if (checkWin())
- {
- _win = Game::EMPTY ;
- board[i][j] = _computer ;
- return ;
- }
- else board [i][j] = Game::EMPTY ;
- }
- }
- }
- if ( board[1][1] == Game::EMPTY)
- {
- board[1][1] = _computer ;
- return ;
- }
- else
- {
- while (_win == Game::EMPTY)
- {
- int x = rand() % 3 ;
- int y = rand() % 3 ;
- if (board[x][y] == Game::EMPTY)
- {
- board[x][y] = _computer ;
- return ;
- }
- }
- }
- }
- void Game::Play2P()
- {
- sf::Event Event ;
- while(_mainWindow.pollEvent (Event))
- {
- DrawGame() ;
- _mainWindow.display () ;
- if (Event.type == sf::Event::Closed ) _gameState = Game::Exiting ;
- if (Event.type == sf::Event::KeyPressed)
- {
- if (Event.key.code == sf::Keyboard::Escape) ShowMenu() ;
- }
- if (Event.type == sf::Event::MouseButtonPressed)
- {
- HandleClick (Event.mouseButton.x, Event.mouseButton.y) ;
- }
- }
- }
- void Game::SetDraw()
- {
- for (int i = 0; i<SIZE ; i++)
- for (int j = 0; j<SIZE ; j++)
- board[i][j] = Game::EMPTY ; //init board
- LoadBackground () ; //load background image
- LoadX() ;
- LoadO() ;
- //set border lines
- for ( int i = 0 ; i < 4 ; i++)
- {
- if (i<2)
- {
- borders[i].setPosition (0, (i+1) * 140) ;
- borders[i].setSize (sf::Vector2f (420, 3)) ;
- }
- else
- {
- borders [i].setPosition ((i - 1)*140, 0) ;
- borders[i].setSize (sf::Vector2f (3, 420)) ;
- }
- borders[i].setFillColor (sf::Color (0, 0 , 0)) ;
- }
- //set Column tine of borders
- lastColumn.setPosition (420, 0) ;
- lastColumn.setSize (sf::Vector2f (3, 559)) ;
- lastColumn.setFillColor (sf::Color ( 0, 0 , 0)) ;
- //set last line of borders
- lastLine.setPosition ( 0, 420 ) ;
- lastLine.setSize (sf::Vector2f (599, 3)) ;
- lastLine.setFillColor (sf::Color (0, 0 , 0)) ;
- //set separator line
- sepLine.setPosition ( 210, 420) ;
- sepLine.setSize (sf::Vector2f ( 3, 180)) ;
- sepLine.setFillColor (sf::Color (0, 0 ,0)) ;
- sf::Font tronFont ;
- if (!tronFont.loadFromFile ("fonts/neon2.ttf"))
- {
- std::cout<<"Couldn't load TRON.ttf font\n";
- tronFont.getDefaultFont() ;
- }
- for (int i =0 ; i < 4; i++)
- {
- //Turn[i].setFont (tronFont) ;
- Turn[i].setCharacterSize(60) ;
- Turn[i].setColor (sf::Color (255, 255, 255)) ;
- Turn[i].setPosition ( 470 , i*100 + 10) ;
- }
- Turn[0].setString ("T") ;
- Turn[1].setString ("U") ;
- Turn[2].setString ("R") ;
- Turn[3].setString ("N") ;
- Reset.setColor (sf::Color (255, 0 ,255)) ;
- Reset.setCharacterSize (60) ;
- Reset.setPosition ( 10 , 500) ;
- Reset.setRotation (-35.0f) ;
- Reset.setString ("Reset") ;
- goMainMenu.setColor (sf::Color (255, 0, 255)) ;
- goMainMenu.setCharacterSize (40) ;
- goMainMenu.setPosition (220, 520) ;
- goMainMenu.setRotation ( - 35.0f) ;
- goMainMenu.setString ("Main Menu") ;
- }
- void Game::DrawGame()
- {
- _mainWindow.draw (backSprite) ;
- //Board::Draw(window) ;
- for (int i = 0; i < 4 ; i++)
- _mainWindow.draw (borders[i]) ;
- _mainWindow.draw (lastLine) ;
- _mainWindow.draw (lastColumn) ;
- _mainWindow.draw (sepLine) ;
- for (int i = 0 ; i < 4 ; i++)
- _mainWindow.draw (Turn[i]) ;
- _mainWindow.draw (Reset) ;
- _mainWindow.draw (goMainMenu) ;
- for (int i = 0 ; i < SIZE ; i++)
- for (int j = 0; j < SIZE ; j++)
- {
- if ( board[i][j] == Game::X)
- {
- xSprite.setPosition (i*140, j*140) ;
- _mainWindow.draw (xSprite) ;
- //Board::_xPiece.SetPosition (i*140, j*140) ;
- //Board::_xPiece.Draw(window) ;
- }
- else if ( board[i][j] == Game::O)
- {
- oSprite.setPosition (i*140, j*140) ;
- _mainWindow.draw (oSprite) ;
- //Board::_oPiece.SetPosition (i*140, j *140) ;
- //Board::_oPiece.Draw (window) ;
- }
- }
- if (_turn == Game::X)
- {
- xSprite.setPosition (420, 420) ;
- _mainWindow.draw (xSprite) ;
- //Board::_xPiece.SetPosition (420, 420) ;
- //Board::_xPiece.Draw(window) ;
- }
- else
- {
- oSprite.setPosition (420,420) ;
- _mainWindow.draw (oSprite) ;
- //Board::_oPiece.SetPosition (420, 420) ;
- //Board::_oPiece.Draw(window) ;
- }
- if (_win == Game::O)
- {
- sf::Text xWin ("X HAS WON") ;
- if (_gameState == Game::Playing1P && _win == _computer) xWin.setString ("O HAS WON") ;
- xWin.setPosition (sf::Vector2f( 20 , 340)) ;
- xWin.setCharacterSize (70) ;
- xWin.setRotation (-45.0f) ;
- for (int i = 0 ;i < 3;i++)
- for (int j = 0 ; j < 3 ; j++)
- if (board[i][j] == Game::EMPTY)
- board[i][j] = 9 ;
- _mainWindow.draw (xWin) ;
- }
- if (_win == Game::X)
- {
- sf::Text oWin ("O HAS WON") ;
- if (_gameState == Game::Playing1P && _win == _computer) oWin.setString ("X HAS WON") ;
- oWin.setPosition (sf::Vector2f( 20 , 340)) ;
- oWin.setCharacterSize (70) ;
- oWin.setRotation (-45.0f) ;
- for (int i = 0 ;i < 3;i++)
- for (int j = 0 ; j < 3 ; j++)
- if (board[i][j] == Game::EMPTY)
- board[i][j] = 9 ;
- _mainWindow.draw (oWin) ;
- }
- if (_win != Game::O && _win!= Game::X)
- {
- _win = Game::DRAW ;
- for (int i = 0; i< 3;i++)
- for (int j = 0; j<3 ;j++)
- if (board[i][j] == Game::EMPTY)
- _win = Game::EMPTY ;
- }
- if (_win == Game::DRAW)
- {
- sf::Text dWin ("IT'S A DRAW") ;
- dWin.setCharacterSize (70) ;
- dWin.setPosition (sf::Vector2f( 20 , 340)) ;
- dWin.setRotation (-45.0f) ;
- _mainWindow.draw (dWin) ;
- }
- }
- void Game::LoadX()
- {
- xTexture.loadFromFile ("images/counters.bmp", sf::IntRect (280, 0 , 140 ,140)) ;
- xSprite.setTexture(xTexture) ;
- }
- void Game::LoadO()
- {
- oTexture.loadFromFile ("images/counters.bmp", sf::IntRect (140, 140, 140, 140)) ;
- oSprite.setTexture(oTexture) ;
- }
- void Game::LoadBackground()
- {
- backTexture.loadFromFile ("images/background.jpg") ;
- backSprite.setTexture(backTexture) ;
- }
- void Game::HandleClick(int x, int y)
- {
- if (x > 0 && x < 420 && y > 0 && y < 420 && board[x/140][y/140] == Game::EMPTY)
- {
- board [x/140][y/140] = _turn ;
- changeTurn() ;
- checkWin() ;
- }
- else if (x > 0 && x < 210 && y > 420 && y < 560)
- {
- for (int i = 0; i< 3 ;i++ )
- for (int j =0 ;j<3 ;j++)
- board[i][j] = Game::EMPTY ;
- _turn = Game::X ;
- _win = Game::EMPTY ;
- ChooseTurn() ;
- DrawGame() ;
- }
- else if (x > 210 && x < 420 && y > 420 && y < 560)
- {
- _gameState = Game::ShowingMenu ;
- for (int i = 0; i< 3 ;i++ )
- for (int j =0 ;j<3 ;j++)
- board[i][j] = Game::EMPTY ;
- _turn = Game::X ;
- _win = Game::EMPTY ;
- ChooseTurn() ;
- DrawGame() ;
- }
- }
- void Game::changeTurn ()
- {
- if (_turn == Game::X)
- _turn = Game::O ;
- else
- _turn = Game::X ;
- }
- bool Game::checkWin()
- {
- if ((board[0][0] == board [0][1] && board [0][0] == board[0][2] && board[0][0] != Game::EMPTY)
- || (board[1][0] == board[1][1] && board[1][0] == board [1][2] && board[1][0] != Game::EMPTY)
- || (board[2][0] == board[2][1] && board[2][0] == board [2][2] && board[2][0] != Game::EMPTY)
- || (board[0][0] == board[1][0] && board[0][0] == board [2][0] && board[0][0] != Game::EMPTY)
- || (board[0][1] == board[1][1] && board[0][1] == board [2][1] && board[0][1] != Game::EMPTY)
- || (board[0][2] == board[1][2] && board[0][2] == board [2][2] && board[0][2] != Game::EMPTY)
- || (board[0][0] == board[1][1] && board[0][0] == board [2][2] && board[0][0] != Game::EMPTY)
- || (board[0][2] == board[1][1] && board[0][2] == board [2][0]) && board[0][2] != Game::EMPTY)
- {
- _win = _turn ;
- return true ;
- }
- return false ;
- }
- Game::GameState Game::_gameState = Uninitialized;
- sf::RenderWindow Game::_mainWindow;
- int Game::board[3][3] ;
- sf::Sprite Game::_sprite ;
- sf::Texture Game::_texture ;
- std::string Game::_filename ;
- sf::RectangleShape Game::borders[4] ;
- sf::RectangleShape Game::lastColumn ;
- sf::RectangleShape Game::lastLine ;
- sf::RectangleShape Game::sepLine ;
- sf::Text Game::Turn [4] ;
- sf::Text Game::Reset ;
- sf::Text Game::goMainMenu ;
- int Game::_win = Game::EMPTY;
- int Game::_turn = Game::X ;
- int Game::_computer ;
- int Game::_player ;
- sf::Sprite Game::xSprite ;
- sf::Sprite Game::oSprite ;
- sf::Sprite Game::backSprite ;
- sf::Texture Game::xTexture ;
- sf::Texture Game::oTexture ;
- sf::Texture Game::backTexture ;
- MainMenu.h
- //include guards
- #ifndef _MAINMENU_H
- #define _MAINMENU_H
- //include needed headers
- #include "SFML/Window.hpp"
- #include "SFML/Graphics.hpp"
- #include <list>
- //the MainMenu class
- class MainMenu
- {
- public:
- //creating all possible menu outcomes
- enum MenuResult {Nothing, Exit, Play1P, Play2P, Credits} ;
- //creating the MenuItem struct
- struct MenuItem
- {
- public:
- sf::IntRect rect;
- MenuResult action ;
- };
- MenuResult Show (sf::RenderWindow&) ; //display the menu
- private:
- MenuResult GetMenuResponse (sf::RenderWindow&) ; //get menu response to user action
- MenuResult HandleClick (int x, int y) ; //handle the clicks
- std::list<MenuItem> _menuItems ; //list containing all menu items
- };
- #endif //_MAINMENU_H
- MainMenu.cpp
- //include needed headers
- #include "stdafx.h"
- #include "mainmenu.h"
- MainMenu::MenuResult MainMenu::Show (sf::RenderWindow& window)
- {
- window.clear (sf::Color (0, 0, 0)) ;
- //create menu with primitives
- sf::RectangleShape lines[4] ;
- for (int i = 0; i < 3 ; i++)
- {
- lines[i].setPosition (0, (i+1) * 140 - 1) ;
- lines[i].setSize (sf::Vector2f (559, 3)) ;
- lines[i].setFillColor (sf::Color (255, 255, 255)) ;
- window.draw (lines[i]) ;
- }
- sf::Text text[5] ;
- text[0].setString ("Play: 1 Player") ;
- text[1].setString ("Play: 2 Players") ;
- text[2].setString ("Credits") ;
- text[3].setString ("Exit") ;
- for (int i = 0; i < 4 ; i++)
- {
- text[i].setColor (sf::Color (255, 255 , 255)) ;
- text[i].setCharacterSize (40) ;
- text[i].setOrigin (text[i].getLocalBounds().width / 2, text[i].getLocalBounds().height / 2) ;
- text[i].setPosition (270.0f, ((140.0f * (i+1)) - 70.0f)) ;
- window.draw(text[i]) ;
- }
- //Setup clickable regions
- //play1p menu item coordinates
- MenuItem play1PButton ;
- play1PButton.rect.top = 1 ;
- play1PButton.rect.height = 138 ;
- play1PButton.rect.left = 0 ;
- play1PButton.rect.width = 559 ;
- play1PButton.action = Play1P ;
- //play2p menu item coordinates
- MenuItem play2PButton ;
- play2PButton.rect.top = 141 ;
- play2PButton.rect.height = 138 ;
- play2PButton.rect.left = 0 ;
- play2PButton.rect.width = 559 ;
- play2PButton.action = Play2P ;
- //credits menu item coordinates
- MenuItem creditsButton ;
- creditsButton.rect.top = 281 ;
- creditsButton.rect.height = 138 ;
- creditsButton.rect.left = 0 ;
- creditsButton.rect.width = 559 ;
- creditsButton.action = Credits ;
- //exit menu item coordinates
- MenuItem exitButton ;
- exitButton.rect.top = 419 ;
- exitButton.rect.height = 138 ;
- exitButton.rect.left = 0 ;
- exitButton.rect.width = 559 ;
- exitButton.action = Exit ;
- _menuItems.push_back (play1PButton) ;
- _menuItems.push_back (play2PButton) ;
- _menuItems.push_back (creditsButton) ;
- _menuItems.push_back (exitButton) ;
- window.display() ;
- return GetMenuResponse (window) ;
- }
- MainMenu::MenuResult MainMenu::HandleClick(int x, int y)
- {
- for (std::list<MenuItem>::iterator it = _menuItems.begin() ; it != _menuItems.end(); it++)
- {
- if (it->rect.contains (x, y))
- return it->action ;
- }
- return Nothing ;
- }
- MainMenu::MenuResult MainMenu::GetMenuResponse (sf::RenderWindow& window)
- {
- sf::Event menuEvent ;
- while (true)
- {
- while (window.pollEvent(menuEvent))
- {
- if(menuEvent.type == sf::Event::MouseButtonPressed)
- {
- return HandleClick (menuEvent.mouseButton.x, menuEvent.mouseButton.y) ;
- }
- if(menuEvent.type == sf::Event::Closed)
- {
- return Exit ;
- }
- }
- }
- }
- Credits.h
- #pragma once
- #include "SFML/Window.hpp"
- #include "SFML/Graphics.hpp"
- class Creditz
- {
- public:
- void Show (sf::RenderWindow& window) ;
- };
- Credits.cpp
- #include "stdafx.h"
- #include "Credits.h"
- void Creditz::Show (sf::RenderWindow& renderWindow)
- {
- sf::Text Text ;
- Text.setCharacterSize (20) ;
- Text.setString(" Game created by Radu Daniel Alexandru, \n student at CNILC Ploiesti\n Thanks to Gamefromscratch.com for all the tutorials") ;
- renderWindow.clear (sf::Color (0, 0, 0)) ;
- renderWindow.draw (Text) ;
- renderWindow.display () ;
- sf::Event Event ;
- while (true)
- {
- while (renderWindow.pollEvent (Event))
- {
- if (Event.type == sf::Event::EventType::KeyPressed
- || Event.type == sf::Event::EventType::MouseButtonPressed
- || Event.type == sf::Event::EventType::Closed )
- {
- return ;
- }
- }
- }
- }
- SplashScreen.h
- #pragma once
- class SplashScreen
- {
- public:
- void Show (sf::RenderWindow &window) ;
- };
- SplashScreen.cpp
- #include "StdAfx.h"
- #include "SplashScreen.h"
- void SplashScreen::Show (sf::RenderWindow &window)
- {
- sf::Texture Texture ;
- if (!Texture.loadFromFile ("images/splashscreen.jpg"))
- {
- return ;
- }
- sf::Sprite sprite(Texture) ;
- window.draw (sprite) ;
- window.display () ;
- sf::Event Event ;
- while (true)
- {
- while (window.pollEvent (Event))
- {
- if (Event.type == sf::Event::EventType::KeyPressed
- || Event.type == sf::Event::MouseButtonPressed
- || Event.type == sf::Event::EventType::Closed)
- {
- return ;
- }
- }
- }
- }
- stdafx.h
- // stdafx.h : include file for standard system include files,
- // or project specific include files that are used frequently, but
- // are changed infrequently
- //
- #pragma once
- #include "targetver.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <tchar.h>
- // TODO: reference additional headers your program requires here
- #include <SFML/System.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- #include <SFML/Audio.hpp>
- #include <map>
- #include <iostream>
- #include <cassert>
- stdafx.cpp
- // stdafx.cpp : source file that includes just the standard includes
- // SimpleTicTacToe.pch will be the pre-compiled header
- // stdafx.obj will contain the pre-compiled type information
- #include "stdafx.h"
- // TODO: reference any additional headers you need in STDAFX.H
- // and not in this file
- targetver.h
- #pragma once
- // The following macros define the minimum required platform. The minimum required platform
- // is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
- // your application. The macros work by enabling all features available on platform versions up to and
- // including the version specified.
- // Modify the following defines if you have to target a platform prior to the ones specified below.
- // Refer to MSDN for the latest info on corresponding values for different platforms.
- #ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
- #define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
- #endif
Advertisement
Add Comment
Please, Sign In to add comment