Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Alex Eden
- // Noughts and Crossess
- // Introduction to C++
- // Year 3 Physics Module
- // 22/01/10
- // The code compiles a game of noughts and crosses
- // with a GUI made from FLTK. Addition files needed
- // are the title.jpg image which should be saved in
- // working directory of the project, and the scorelist
- //
- // Filename
- #include <iostream>
- #include <string>
- #include <limits>
- #include <fl.H>
- #include <iomanip>
- #include <fl_window.H>
- #include <fl_button.H>
- #include <fl_box.H>
- #include <FL/Fl_Text_Display.H>
- #include <cmath>
- #include <FL/Fl_JPEG_Image.H>
- #include <FL/Fl_Menu_Bar.H>
- #include <stdlib.h>
- using namespace std;
- //// Declerations /////
- // Declare Strings
- //Player 1s Name is input from user
- string name1("Alex");
- //Player 2s Name is input from user
- string name2("Eden");
- //Part of the string for turn statement
- string extra("'s Turn.");
- string player1(name1+extra);
- string player2(name2+extra);
- //Part of string for win statement
- string win(" wins!");
- /*By adding the players name and string win
- it is possible to output the 'Name wins!'*/
- string player1win(name1+win);
- string player2win(name1+win);
- string inputstring;
- // Declare Arrays //
- // The array needed to input 9 values
- Fl_Button *but[3][3]; //Creates array of 9 buttons for user
- int z = 0; // Used as a switch as there cannot be a tie and a win/loose
- int x = 0; // Used as a switch, if player has input a position already taken
- int player = 1; // Determines Player 1=X 0=O
- int turn = 0; // Counts how many turns have been taken
- int playernumber = 2;
- /*determines number of players playing.
- if 2, 2 players are playing, if 1 one player
- is playing and other player needs to be swapped
- to the computer*/
- char grid[3][3]= { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- void drawgrid(){
- cout << "\n " << grid[1][1] << " | " << grid[1][2] << " | " << grid[1][3] << endl;
- cout<< " --+---+--" << "\n ";
- cout<< grid[2][1] << " | " << grid[2][2] << " | " << grid[2][3] << endl;
- cout<< " --+---+--" << "\n ";
- cout<< grid[3][1] << " | " << grid[3][2] << " | " << grid[3][3] << endl;
- }
- // FLTK Declerations //
- // Main Window
- Fl_Button *quit_but;
- Fl_Box *main_frame; //Frame to highlight playing area
- Fl_Box *title_box; //Box to put title image in
- Fl_Window *main_win = new Fl_Window(400, 435, "Noughts and Crosses - Main Menu"); //Playing window
- Fl_Text_Buffer *output_buff = new Fl_Text_Buffer(); //Text buffer for output box
- Fl_Text_Display *disp_output = new Fl_Text_Display(40, 305, 120, 40); //Displays output
- Fl_Text_Buffer *input_box = new Fl_Text_Buffer();
- /*Quits Programme by returning 0 to int main
- This is access via the menu and by exit_but*/
- void quit_cb(Fl_Widget *, void *) {
- exit(0);
- }
- bool checkwinner();
- /*Function resets the grid by putting the buttons
- to their inital attributes. The buttons are
- reactivated and turned back to gray.
- Accessed via the menu*/
- void grid_reset(Fl_Widget *, void *){
- for (int a=1; a<=3; a++){
- for(int b=1; b<=3; b++){
- but[a][b]->label(0);
- but[a][b]->color(FL_GRAY);
- but[a][b]->activate();
- char grid[3][3]= { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- }
- }
- }
- // Deactivates button and sets character
- void grid_cb(Fl_Widget *obj, long int number){
- int b= number/10;
- int a= number%10;
- if(player == 1){
- obj->labelcolor(FL_WHITE);
- obj->label("X");
- obj->color(FL_DARK_BLUE);
- obj->deactivate();
- output_buff->text(player2.c_str());
- grid[a][b] = 'X';
- drawgrid();
- cout << checkwinner();
- turn++;
- player = 0;
- }
- else if(player == 0){
- obj->labelcolor(FL_DARK_BLUE);
- obj->label("O");
- obj->color(FL_WHITE);
- obj->deactivate();
- output_buff->text(player1.c_str());
- grid[a][b] = 'O';
- drawgrid();
- cout << checkwinner();
- turn++;
- player = 1;
- }
- }
- //}
- /* void inputbutton_cb(Fl_Widget* w, void* data) {
- int state = 0; //Allows for different function on each click
- if (state == 0){
- inputstring = input_box->text();
- name1 ->text(inputstring.c_str());
- input_box->value(NULL);
- if (playernumber == 2){
- state = 1;
- }
- else if (playernumber == 1){
- input_box->hide();
- }
- if (state == 0){
- inputstring = input_box->value();
- name2 ->value(inputstring.c_str());
- input_box->value(NULL);
- input_box->hide();
- }
- }
- */
- void winner(Fl_Widget *, void *){
- but[1][1]->deactivate();
- but[1][2]->deactivate();
- but[1][3]->deactivate();
- but[2][1]->deactivate();
- but[2][2]->deactivate();
- but[2][3]->deactivate();
- but[3][1]->deactivate();
- but[3][2]->deactivate();
- but[3][3]->deactivate();
- }
- //// End of Declerations ////
- //// Main Programme ////
- int main () {
- main_win;
- main_win->color(FL_DARK_BLUE);
- main_frame = new Fl_Box(20, 115, 360,300);
- main_frame->box(FL_ROUNDED_BOX);
- main_frame->color(FL_BLUE);
- for (int a=1; a<=3; a++){
- for(int b=1; b<=3; b++){
- but[a][b]=new Fl_Button( a*40, (b*40)+95, 40, 40);
- but[a][b]->callback(grid_cb, 10*a+b);
- }
- }
- Fl_Box title_box(0,25,400,85);
- Fl_JPEG_Image jpg("title.jpg");
- title_box.image(jpg);
- Fl_Text_Buffer *rules_buff = new Fl_Text_Buffer();
- Fl_Text_Display *disp_rules = new Fl_Text_Display(200, 135, 160, 260, "Rules");
- disp_rules->labelcolor(FL_WHITE);
- disp_rules->labelfont(FL_BOLD);
- rules_buff->text("\nWelcome to Noughts\nand Crosses!\n"
- "\n1) Select one or two \nplayers."
- "\n\n2) Enter player \nname/s."
- "\n\n3) Play the game!"
- "\n \nYou can change the"
- "\ndifficulty of the"
- "\ncomputer and view "
- "\nthe rules using "
- "\nthe menu bar. ");
- disp_rules->buffer(rules_buff);
- main_win->resizable(*disp_rules);
- disp_output->buffer(output_buff);
- output_buff->text("Enter Player 1's Name");
- quit_but = new Fl_Button (40,355,120 ,40, "Exit");
- quit_but->callback(quit_cb);
- Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25);
- menu->add("&Game/New Game", FL_CTRL+'n', grid_reset,0, FL_MENU_DIVIDER);
- menu->add("Game/1 Player");
- menu->add("Game/2 Player", 0, 0, 0, FL_MENU_DIVIDER);
- menu->add("Game/Quit", FL_CTRL+'q', quit_cb);
- menu->add("&Difficulty/Easy");
- menu->add("Difficulty/Hard");
- menu->add("&Help/Rules");
- menu->add("Help/About");
- main_win->show();
- main_win->end();
- if (checkwinner()){
- winner;
- }
- drawgrid();
- Fl::run();
- // cout << checkwinner() ;
- }
- // cout << "Enter Player 1's Name: ";
- // cin >> name1;
- // cout << "Enter Player 2's Name: ";
- // cin >> name2;
- // drawgrid();
- // bool player = false;
- // int i = 0;
- // while(!checkwinner()){
- // if (i==0 || i==2 || i==4 || i ==6 || i==8){
- // i++;{
- // if (player == true)
- // player = false;
- // else
- // player = true;
- // moveplayer1(player);
- // }
- // }
- // else if (i==1 || i==3 || i==5 || i ==7){
- // i++;{
- // if (player == true)
- // player = false;
- // else
- // player = true;
- // moveplayer2(player);
- // }
- // }
- // else if (i==9 && checkwinner() == false){
- // cout << "\nThe game is tied. " << endl;
- // z = 1;
- // break;
- // }
- // }
- // if(player == true && z == 0 ){
- // cout << name1 << " wins the game!" << endl;
- // }
- // else if (player == false && z == 0 ){
- // cout << name2 << " wins the game!" << endl;
- // }
- //}
- //// End of Main Programme ////
- // Declare Functions //
- /* Moves Player 1
- void moveplayer1(bool whichplayer){
- if (x == 1){
- cout << " \n Place has already been taken! Try again.";
- }
- while ((cout << "\n " << name1 << "'s turn: ") && !(cin >> position) ||
- (position < 1) || (position > 9)) {
- cout << "Must be an integer between 1 and 9." << endl;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- if (checkgrid(position)){
- if(whichplayer == true){
- grid[position-1] = 'X';
- x = 0;
- drawgrid();
- }
- else{
- grid[position-1] = 'O';
- x = 0;
- drawgrid();
- }
- }
- else{
- x = 1;
- drawgrid();
- moveplayer1(whichplayer);
- }
- }
- */
- /*This function searches through the grid to check for any winning
- combinations. It does this by first searching rows, then the
- columns and finally the diagonal winning positions. If true
- then the game is stopped and won by the player whose turn it is*/
- bool checkwinner(){
- if (grid[1][1] == grid[1][2] && grid[1][2] == grid[1][3])
- return true;
- else if (grid[2][1] == grid[2][2] == grid[2][3])
- return true;
- else if (grid[3][1] == grid[3][2] && grid[2][3])
- return true;
- else if (grid[1][1] == grid[2][1] == grid[3][1])
- return true;
- else if (grid[1][2] == grid[2][2] && grid[3][2])
- return true;
- else if (grid[1][3] == grid[2][3] && grid[3][3])
- return true;
- else if (grid[1][1] == grid[2][2] && grid[3][3])
- return true;
- else if (grid[1][3] == grid[2][2] && grid[1][3])
- return true;
- else
- return false;
- }
Add Comment
Please, Sign In to add comment