• Post Reply Bookmark Topic Watch Topic
  • New Topic

GOTO statement  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I'm bored and I'm making a quick and simple x and o's console game and I game to a dilemma in c and c++ I could get around this dilemma with the GOTO statement I know it's frowned upon using it even in them languages but that statement would make sense in that case so the problem occurs at line 62 to 66 after this block is finished I would like to loop back to line 56 so the user gets a chance to enter a choice in again if he/she entered an invalid choice in,I'm not sure how I could get around this without a GOTO statement any suggestions ?

thanks here is the code

 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use a loop, probably a do/while loop.  You'd have to refactor your code somewhat, but the basic idea would be this:
 
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant believe how much I have changed. I used to think a GOTO was not a problem. I don't believe that any more. I posted a rant saying we should dump OO and just go top-down procedural when I was a new member here
 
Ranch Foreman
Posts: 3074
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a pointer, the code in playerOneTurn and playerTwoTurn is almost identical, the only difference being whether it uses an 'X' or an 'O', and whether it talks about player 'One' or 'Two'.
You could turn that into a single method with one or two parameters.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you wouldn't want players as parameters. There shou‍ld be player classes and objects. Each player would take turns, rather than that being done by static methods in another class.
 
Dave Tolls
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would be the next step.

Rather than passing in the symbol and "player name" (ie 'One' or 'Two') the method would be on an object of a Player class that would already know who they were, and what symbol they were playing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!