Game Of Nim In Java
LAB: Game of Nim Game of NimThe Rules of the Game:Nim is a two player game in which players alternate making moves. Initially the players are given several piles of sticks.
Each pile can have any number of sticks in it. On each turn a player can remove any number of sticks from one pile.
- Design and implement a Nim class and a NimGame application to allow two human players to play the game of Nim. The game starts with four piles of stones. The first pile has 3 stones, the second pile has 5 stones, the third pile has 7 stones, and the last pile has 9 stones. The players take turns removing stones.
- Amazon T here are many variations of the game of Nim. In this program you are to allow the user to play this game with the computer. The game starts with an initial number of marbles, which your program decides, and determines at random who goes first, and then the user and the computer take turns.
The player must remove a minimum of one stick from the pile or may remove the whole pile. The player who picks up the last stick loses.The name 'Nim' itself comes from the imperative form of the German word 'nehmen', meaning 'to take'.Nim is a dominated game, which means that there is a winning strategy for one of the players, regardless of the opponent's moves. In this assignment you are to build a decision tree and demonstrate a winning strategy (if there is such) for the first player.Assignment:You read in the input file, build a GameTree and determine whether or not there is a winning strategy for a player who makes the first move.You are to design and implement the GameNode class. The class should have the following fields (you may include any other fields that you find useful)private ArrayList piles;This array represents the number of sticks in each pile after a player's move.private ArrayList children;This represents a list of references to all children.private boolean myTurn;Since players alternate moves you need to keep a track of player's turns.
Design and implement a Nim class and a NimGame application to allow two human players to play the game of Nim. The game starts with four piles of stones. The first pile has 3 stones, the second pile has 5 stones, the third pile has 7 stones, and the last pile has 9 stones.
MyTurn returns true if it the first player's move.private boolean win;It returns true if there is a winning strategy for the first player for the current state of piles.requirements for this class. The piles and myTurn fields must be immutable. You have to make sure that a parent node has no identical children. You might want to use the API's.You are to implement the GameTree class with the following fieldsprivate GameNode root;This represents the root node in the tree.and methodspublic GameTree(String infile)This creates a tree of one node (root) by reading in the input file.public void buildTreeThis method creates (perhaps recursively) a full game tree.public boolean evaluateThis method returns true if there is a guaranteed winning strategy for the first player.
You start with assigning false (it’s a losing outcome) to leaves (the TreeNode class has a designated field win for this purpose) on levels corresponding to the first player's move and true (it’s a winning outcome for the first player) to leaves on levels corresponding to the opponent's move. Then you assign true/false to the internal nodes by traversing the tree in postorder. If the parent node represents the opponent’s move, we apply AND to children's win fields. If the parent node represents the first player's move, we apply OR.public LinkedList getWinningGamePathIf there exists a winning strategy, this method returns a linked List of the GameNodes associated with a winning strategy for the first player.
To find the winning strategy you should follow “true”s, starting from the root. You can choose any winning branch of the tree. If no winning strategy exists, the method must return the root node. You can use either your own or the API's LinkedList class.public int countNodesThis methods returns the total number of nodes in the tree.Feel free to add any helper methods to the GameTree class.Input File:The file begins with a sequence of integers representing the number of sticks in each pile followed by a blank line. For example:1 3 5 7 9Testing:I advise you to start with 1 pile.
Once that works, try 2 piles. If the piles become larger (and so do the tree), you'll run out of memory. On my laptop, the program ran out of memory when tested on 2 piles with sizes 8,8; on 3 piles with sizes 3,4,5; and on 4 piles with sizes 2,3,3,3. The method in the main driver nimTest will test whether your solution is correct. Your TAs will be using this function and their own input files to test your solution.Extra Credit (up to +10 each):. Create a NIM game simulator (build GUI) that will match an intelligent computer player against a human player. Demonstrate efficient memory (and speed) management by using dynamic programming - use a hash table of some kind to avoid storing the same nodes (or branches).
This will allow you to build a full tree for 5 piles with sizes 1 2 3 4 5 in reasonable time.What You'll Need:Download the. Save it to your temp directory and unzip the files.
You should see the following files:. lab.html. Nim.mcp. MainDriver.java. GameNode.java. GameTree.javaHanding in your Solution:ftp your solution to unix.andrew.cmu.eduGood luck playing!
Game Of Nim Java Code
Mr.Volynsky Alex is a Software Engineer in a leading software company. Alex is skilled in many areas of computer science. He has over 14 years of experience in the design & development of applications using C/C/STL, Qt, MFC, DirectShow, JavaScript, VBScript, Bash and of course - C#/.NET.In addition, Alex is the (he was awarded by Intel® Green Belt for his active contribution to the Intel Developer Zone community for developers using Intel technology).Alex is also interested in the Objective-C development for the iPad/iPhone platforms and he is the developer of the free on the App Store.Overall, Alex is very easy to work with. He adapts to new systems and technology while performing complete problem definition research.His hobbies include yacht racing, photography and reading in multiple genres.He is also fascinated by attending computer meetings in general, loves traveling, and also takes pleasure in exercising and relaxing with friends.Visit his C 11.