@@ -22,31 +22,38 @@ void Table::settings()
2222 // getting the choice
2323 int choice;
2424 cin >> choice;
25- setGameMode (choice);
2625 // checking the choice
27- if (getGameMode () != 1 && getGameMode () != 2 && getGameMode () != 3 && getGameMode () != 4 )
26+ try
27+ {
28+ if (choice != 1 && choice != 2 && choice != 3 && choice != 4 )
29+ {
30+ throw " Invalid game mode" ;
31+ }
32+ }
33+ catch (const char * msg)
2834 {
29- cout << " Invalid choice " << endl ;
35+ std::cerr << msg << ' \n ' ;
3036 sleep (2 );
31- exit (0 );
37+ exit (EXIT_FAILURE );
3238 }
39+ setGameMode (choice);
3340 // in case of 1, we let the user choose the side
3441 if (getGameMode () == 1 )
3542 {
3643 cout << " \n Enter 'b' to play BLACK, 'w' to play WHITE\n " ;
3744 char side;
3845 cin >> side;
39- setUserSide (side);
4046
41- if (getUserSide () == ' b' || getUserSide () == ' B' )
47+ if (side == ' b' || side == ' B' )
4248 setUserSide (BLACK);
4349
44- else if (getUserSide () == ' w' || getUserSide () == ' W' )
50+ else if (side == ' w' || side == ' W' )
4551 setUserSide (WHITE);
4652
4753 else
4854 {
4955 cout << " Invalid choice!!!" ;
56+ sleep (2 );
5057 exit (EXIT_FAILURE);
5158 }
5259 }
@@ -57,19 +64,35 @@ void Table::settings()
5764 cin >> fileName;
5865 fstream file;
5966 // open the file in read mode
60- file.open (fileName, ios::in);
61- if (!file.is_open ())
67+ try
68+ {
69+ file.open (fileName, ios::in);
70+ if (!file.is_open ())
71+ {
72+ throw " File not found" ;
73+ sleep (2 );
74+ exit (EXIT_FAILURE);
75+ }
76+ }
77+ catch (const char * msg)
6278 {
63- cout << " File not found!!! " ;
79+ std::cerr << msg << ' \n ' ;
6480 sleep (2 );
6581 exit (EXIT_FAILURE);
6682 }
6783 file.close ();
6884 // check if the file has valid data
6985 this ->moves = loadFromFile (fileName);
70- if (!checkFile ())
86+ try
87+ {
88+ if (!checkFile ())
89+ {
90+ throw " Invalid file" ;
91+ }
92+ }
93+ catch (const char * msg)
7194 {
72- cout << " The file is not valid " ;
95+ std::cerr << msg << ' \n ' ;
7396 sleep (2 );
7497 exit (EXIT_FAILURE);
7598 }
0 commit comments