Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Nim in Java  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do I need to change so the player who takes the last one become a winner.

 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked  at all your code yet but this  jumped out at me:
You should never create more than one instance of a Scanner from System.in within an ENTIRE PROJECT. Scanner has a small amount of buffering built in which could cause one Scanner to be waiting for input that another Scanner has already consumed.

You have one large method which will probably need to be broken down into smaller methods, so, rather than worrying about passing a Scanner variable around it would be simplified if you made a constant out of it.
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start over and begin your next class look something like this:
This is just off the top of my head so there may be better ways to do it, but I think you get the point.  The methods playerPicksUpSticks(), computerPicksUpSticks(), and checkForWinner() are up to you to write.  Remember the DRY principle: don't repeat yourself.
 
Saloon Keeper
Posts: 2195
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from what Knute and Carey stated, I would want to point out that it's not good to write the entire game logic in a single main method. Check out this link : Main Is A Pain
 
Ranch Hand
Posts: 505
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Comments like this are silly.  Comments should explain an algorithm, explain why you're doing something, or refer to a requirement.  They should not simply restate the code.

Offtopic, but I remember reading something along these lines once:

And the classic /* you aren't expected to understand this */.   If memory serves, this was in the original Unix kernel that was using a hardware quirk in a PDP-11.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!