Guest User

Untitled

a guest
Jul 22nd, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. // Name: Alex Eden
  2. // Noughts and Crossess
  3. // Introduction to C++
  4. // Year 3 Physics Module
  5. // 22/01/10
  6. // The code compiles a game of noughts and crosses
  7. // with a GUI made from FLTK. Addition files needed
  8. // are the title.jpg image which should be saved in
  9. // working directory of the project, and the scorelist
  10. //
  11. // Filename
  12.  
  13. #include <iostream>
  14. #include <string>
  15. #include <limits>
  16. #include <fl.H>
  17. #include <iomanip>
  18. #include <fl_window.H>
  19. #include <fl_button.H>
  20. #include <fl_box.H>
  21. #include <FL/Fl_Text_Display.H>
  22. #include <cmath>
  23. #include <FL/Fl_JPEG_Image.H>
  24. #include <FL/Fl_Menu_Bar.H>
  25. #include <stdlib.h>
  26. using namespace std;
  27.  
  28. //// Declerations /////
  29.  
  30.  
  31. // Declare Strings
  32. //Player 1s Name is input from user
  33. string name1("Alex");
  34.  
  35. //Player 2s Name is input from user
  36. string name2("Eden");
  37.  
  38. //Part of the string for turn statement
  39. string extra("'s Turn.");
  40.  
  41.  
  42. string player1(name1+extra);
  43. string player2(name2+extra);
  44.  
  45. //Part of string for win statement
  46. string win(" wins!");
  47.  
  48. /*By adding the players name and string win
  49. it is possible to output the 'Name wins!'*/
  50. string player1win(name1+win);
  51. string player2win(name1+win);
  52. string inputstring;
  53.  
  54. // Declare Arrays //
  55.  
  56. // The array needed to input 9 values
  57.  
  58. Fl_Button *but[3][3]; //Creates array of 9 buttons for user
  59. int z = 0; // Used as a switch as there cannot be a tie and a win/loose
  60. int x = 0; // Used as a switch, if player has input a position already taken
  61. int player = 1; // Determines Player 1=X 0=O
  62. int turn = 0; // Counts how many turns have been taken
  63. int playernumber = 2;
  64.  
  65.  
  66.  
  67.  
  68.  
  69. /*determines number of players playing.
  70. if 2, 2 players are playing, if 1 one player
  71. is playing and other player needs to be swapped
  72. to the computer*/
  73.  
  74. char grid[3][3]= { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  75.  
  76. void drawgrid(){
  77. cout << "\n " << grid[1][1] << " | " << grid[1][2] << " | " << grid[1][3] << endl;
  78. cout<< " --+---+--" << "\n ";
  79. cout<< grid[2][1] << " | " << grid[2][2] << " | " << grid[2][3] << endl;
  80. cout<< " --+---+--" << "\n ";
  81. cout<< grid[3][1] << " | " << grid[3][2] << " | " << grid[3][3] << endl;
  82. }
  83. // FLTK Declerations //
  84.  
  85. // Main Window
  86. Fl_Button *quit_but;
  87. Fl_Box *main_frame; //Frame to highlight playing area
  88. Fl_Box *title_box; //Box to put title image in
  89. Fl_Window *main_win = new Fl_Window(400, 435, "Noughts and Crosses - Main Menu"); //Playing window
  90. Fl_Text_Buffer *output_buff = new Fl_Text_Buffer(); //Text buffer for output box
  91. Fl_Text_Display *disp_output = new Fl_Text_Display(40, 305, 120, 40); //Displays output
  92. Fl_Text_Buffer *input_box = new Fl_Text_Buffer();
  93.  
  94. /*Quits Programme by returning 0 to int main
  95. This is access via the menu and by exit_but*/
  96. void quit_cb(Fl_Widget *, void *) {
  97. exit(0);
  98. }
  99.  
  100. bool checkwinner();
  101.  
  102. /*Function resets the grid by putting the buttons
  103. to their inital attributes. The buttons are
  104. reactivated and turned back to gray.
  105. Accessed via the menu*/
  106. void grid_reset(Fl_Widget *, void *){
  107. for (int a=1; a<=3; a++){
  108. for(int b=1; b<=3; b++){
  109. but[a][b]->label(0);
  110. but[a][b]->color(FL_GRAY);
  111. but[a][b]->activate();
  112. char grid[3][3]= { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  113. }
  114. }
  115. }
  116.  
  117. // Deactivates button and sets character
  118. void grid_cb(Fl_Widget *obj, long int number){
  119. int b= number/10;
  120. int a= number%10;
  121.  
  122. if(player == 1){
  123. obj->labelcolor(FL_WHITE);
  124. obj->label("X");
  125. obj->color(FL_DARK_BLUE);
  126. obj->deactivate();
  127. output_buff->text(player2.c_str());
  128. grid[a][b] = 'X';
  129. drawgrid();
  130. cout << checkwinner();
  131. turn++;
  132. player = 0;
  133. }
  134. else if(player == 0){
  135. obj->labelcolor(FL_DARK_BLUE);
  136. obj->label("O");
  137. obj->color(FL_WHITE);
  138. obj->deactivate();
  139. output_buff->text(player1.c_str());
  140. grid[a][b] = 'O';
  141. drawgrid();
  142. cout << checkwinner();
  143. turn++;
  144. player = 1;
  145. }
  146. }
  147.  
  148. //}
  149.  
  150.  
  151. /* void inputbutton_cb(Fl_Widget* w, void* data) {
  152. int state = 0; //Allows for different function on each click
  153.  
  154. if (state == 0){
  155. inputstring = input_box->text();
  156. name1 ->text(inputstring.c_str());
  157. input_box->value(NULL);
  158. if (playernumber == 2){
  159. state = 1;
  160. }
  161. else if (playernumber == 1){
  162. input_box->hide();
  163. }
  164.  
  165. if (state == 0){
  166. inputstring = input_box->value();
  167. name2 ->value(inputstring.c_str());
  168. input_box->value(NULL);
  169. input_box->hide();
  170. }
  171. }
  172. */
  173.  
  174. void winner(Fl_Widget *, void *){
  175. but[1][1]->deactivate();
  176. but[1][2]->deactivate();
  177. but[1][3]->deactivate();
  178. but[2][1]->deactivate();
  179. but[2][2]->deactivate();
  180. but[2][3]->deactivate();
  181. but[3][1]->deactivate();
  182. but[3][2]->deactivate();
  183. but[3][3]->deactivate();
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190. //// End of Declerations ////
  191.  
  192. //// Main Programme ////
  193.  
  194. int main () {
  195.  
  196. main_win;
  197. main_win->color(FL_DARK_BLUE);
  198.  
  199. main_frame = new Fl_Box(20, 115, 360,300);
  200. main_frame->box(FL_ROUNDED_BOX);
  201. main_frame->color(FL_BLUE);
  202.  
  203. for (int a=1; a<=3; a++){
  204. for(int b=1; b<=3; b++){
  205. but[a][b]=new Fl_Button( a*40, (b*40)+95, 40, 40);
  206. but[a][b]->callback(grid_cb, 10*a+b);
  207. }
  208. }
  209.  
  210. Fl_Box title_box(0,25,400,85);
  211. Fl_JPEG_Image jpg("title.jpg");
  212. title_box.image(jpg);
  213.  
  214. Fl_Text_Buffer *rules_buff = new Fl_Text_Buffer();
  215. Fl_Text_Display *disp_rules = new Fl_Text_Display(200, 135, 160, 260, "Rules");
  216. disp_rules->labelcolor(FL_WHITE);
  217. disp_rules->labelfont(FL_BOLD);
  218.  
  219. rules_buff->text("\nWelcome to Noughts\nand Crosses!\n"
  220. "\n1) Select one or two \nplayers."
  221. "\n\n2) Enter player \nname/s."
  222. "\n\n3) Play the game!"
  223. "\n \nYou can change the"
  224. "\ndifficulty of the"
  225. "\ncomputer and view "
  226. "\nthe rules using "
  227. "\nthe menu bar. ");
  228. disp_rules->buffer(rules_buff);
  229. main_win->resizable(*disp_rules);
  230.  
  231. disp_output->buffer(output_buff);
  232. output_buff->text("Enter Player 1's Name");
  233.  
  234.  
  235.  
  236.  
  237.  
  238. quit_but = new Fl_Button (40,355,120 ,40, "Exit");
  239. quit_but->callback(quit_cb);
  240.  
  241. Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25);
  242. menu->add("&Game/New Game", FL_CTRL+'n', grid_reset,0, FL_MENU_DIVIDER);
  243. menu->add("Game/1 Player");
  244. menu->add("Game/2 Player", 0, 0, 0, FL_MENU_DIVIDER);
  245. menu->add("Game/Quit", FL_CTRL+'q', quit_cb);
  246. menu->add("&Difficulty/Easy");
  247. menu->add("Difficulty/Hard");
  248. menu->add("&Help/Rules");
  249. menu->add("Help/About");
  250.  
  251. main_win->show();
  252. main_win->end();
  253.  
  254. if (checkwinner()){
  255. winner;
  256. }
  257. drawgrid();
  258.  
  259. Fl::run();
  260. // cout << checkwinner() ;
  261. }
  262.  
  263.  
  264.  
  265. // cout << "Enter Player 1's Name: ";
  266. // cin >> name1;
  267. // cout << "Enter Player 2's Name: ";
  268. // cin >> name2;
  269. // drawgrid();
  270.  
  271.  
  272. // bool player = false;
  273. // int i = 0;
  274.  
  275. // while(!checkwinner()){
  276. // if (i==0 || i==2 || i==4 || i ==6 || i==8){
  277. // i++;{
  278. // if (player == true)
  279. // player = false;
  280. // else
  281. // player = true;
  282. // moveplayer1(player);
  283. // }
  284. // }
  285. // else if (i==1 || i==3 || i==5 || i ==7){
  286. // i++;{
  287. // if (player == true)
  288. // player = false;
  289. // else
  290. // player = true;
  291. // moveplayer2(player);
  292. // }
  293. // }
  294. // else if (i==9 && checkwinner() == false){
  295. // cout << "\nThe game is tied. " << endl;
  296. // z = 1;
  297. // break;
  298. // }
  299. // }
  300. // if(player == true && z == 0 ){
  301. // cout << name1 << " wins the game!" << endl;
  302. // }
  303. // else if (player == false && z == 0 ){
  304. // cout << name2 << " wins the game!" << endl;
  305. // }
  306. //}
  307.  
  308.  
  309.  
  310. //// End of Main Programme ////
  311.  
  312.  
  313. // Declare Functions //
  314.  
  315.  
  316. /* Moves Player 1
  317. void moveplayer1(bool whichplayer){
  318. if (x == 1){
  319. cout << " \n Place has already been taken! Try again.";
  320. }
  321. while ((cout << "\n " << name1 << "'s turn: ") && !(cin >> position) ||
  322. (position < 1) || (position > 9)) {
  323. cout << "Must be an integer between 1 and 9." << endl;
  324. cin.clear();
  325. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  326. }
  327. if (checkgrid(position)){
  328. if(whichplayer == true){
  329. grid[position-1] = 'X';
  330. x = 0;
  331. drawgrid();
  332. }
  333. else{
  334. grid[position-1] = 'O';
  335. x = 0;
  336. drawgrid();
  337. }
  338. }
  339. else{
  340. x = 1;
  341. drawgrid();
  342. moveplayer1(whichplayer);
  343. }
  344. }
  345. */
  346.  
  347. /*This function searches through the grid to check for any winning
  348. combinations. It does this by first searching rows, then the
  349. columns and finally the diagonal winning positions. If true
  350. then the game is stopped and won by the player whose turn it is*/
  351. bool checkwinner(){
  352. if (grid[1][1] == grid[1][2] && grid[1][2] == grid[1][3])
  353. return true;
  354.  
  355. else if (grid[2][1] == grid[2][2] == grid[2][3])
  356. return true;
  357.  
  358. else if (grid[3][1] == grid[3][2] && grid[2][3])
  359. return true;
  360.  
  361. else if (grid[1][1] == grid[2][1] == grid[3][1])
  362. return true;
  363.  
  364. else if (grid[1][2] == grid[2][2] && grid[3][2])
  365. return true;
  366.  
  367. else if (grid[1][3] == grid[2][3] && grid[3][3])
  368. return true;
  369.  
  370. else if (grid[1][1] == grid[2][2] && grid[3][3])
  371. return true;
  372.  
  373. else if (grid[1][3] == grid[2][2] && grid[1][3])
  374. return true;
  375.  
  376. else
  377. return false;
  378. }
Add Comment
Please, Sign In to add comment