Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Java Programming 24-Hour Trainer by Yakov Fain
This is the forum to discuss the Wrox book Java Programming 24-Hour Trainer by Yakov Fain; ISBN: 978-0-470-88964-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Java Programming 24-Hour Trainer by Yakov Fain section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 26th, 2012, 02:50 PM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 11 TryIt

Has anyone figured out how to create the computer opponent for the try it out section in Chapter 11?
  #2 (permalink)  
Old February 27th, 2012, 11:24 PM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Solved!!!

Ha! I finally figured out the AI and it actually makes decisions based on what you select. Now for Chapter 12!
  #3 (permalink)  
Old February 29th, 2012, 02:32 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default

I implemented a method

Code:
private int checkTwoInARow(){

}
which returns the position for the computer to move, if the human opponent has two marks in a row. Otherwise the computer picks a random position for its move.

Using this strategy, with the human starting the game, usually there is a tie or the human wins. I wonder if it is theoretically possible for the computer to win (assuming that the human uses the same strategy), there is no fun otherwise.

I think it would be interesting to alternate the players at starting the game and count the results over several rounds; also to program two computer players to compete against each other.
  #4 (permalink)  
Old March 1st, 2012, 06:14 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Lightbulb

I'm now working on a version implementing the Strategy Pattern:

Code:
public class TicTacToeStrategy {
	
	public static void main(String[] args){
	
	PlayStrategy minimax = new Minimax();
	PlayStrategy aleator = new Aleator();
	
	Player playerX = new Player();
	Player playerY = new Player();
	
	playerX.setStrategy(minimax);
	playerY.setStrategy(aleator);
	
	playerX.computerMove();
	playerY.computerMove();
	}
		
}

class Player{
	private PlayStrategy strategy;
	
	public void setStrategy(PlayStrategy playStrategy){
		strategy = playStrategy;
	}
	
	public void computerMove(){
	strategy.computerMove();
	}
}

interface PlayStrategy{
	public int computerMove();
}
But implementing a minimax strategy seems difficult If I had been able to read Knuth's Art of Computer Programming, I would have probably been working for Microsoft
  #5 (permalink)  
Old March 1st, 2012, 10:54 AM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is this pattern for the computers AI? For my program I just have my computer choosing squares randomly but if I wanted to improve the AI I would just implement if/else statements to check the squares. I also added the ability to have two players or player vs computer using radio buttons.
  #6 (permalink)  
Old March 1st, 2012, 01:20 PM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default The Strategy Design Pattern

The Strategy Pattern is one of the 23 original software design patterns, which are solutions to programming problems that automatically implement good design techniques. Design patterns extend OOP.

The idea behind the Strategy Pattern is to extract code that handles specific tasks from the application and choose (even at runtime) which algorithms you want to work with. You do this by creating a family of algorithms that implement an interface, customizing code by creating composites of objects and using polimorphism to choose the object you want to work with. The code included in my previous message illustrates this.

The code that I want to extract from the application is the method computerMove(), so I created a family of algoritms (interface PlayStrategy) and provided each Player instance with a private variable representing the strategy (object composition). Polimorphism ensures that each player uses its own strategy, though both invoke the same method: computerMove().

Now if you want to define your own strategy, all you have to do is provide an implementation of the interface PlayStrategy. This could be ranging from choosing squares randomly to some sophisticated AI heuristics, going through implementing if/else statements to check the squares.

This is only the pattern. The real work starts from here. If I end up with some interesting solution, I will upload it here: http://code.google.com/p/practicaljava/

I like your idea of using radio buttons to select alternative ways of playing the game, I will try that myself.
  #7 (permalink)  
Old March 2nd, 2012, 09:39 AM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is that your code Java page and how long have you been programming in Java? If you did that yourself thanks! That is a huge contribution for people like me just getting started with Java. I will read those sources once I am done with this book.
  #8 (permalink)  
Old March 2nd, 2012, 10:13 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Default

No, of course it's not mine. It's the page where the author maintains the updated version of the code and solutions to exercises, as you can read here:
http://www.wrox.com/WileyCDA/WroxTit...-DOWNLOAD.html
But you can have your own "clone", which is visible for the public, and upload your solutions there.

I have no industrial experience with Java, only academic. I want to become a developer and I find the practical approach of this book very useful.
  #9 (permalink)  
Old March 3rd, 2012, 01:57 PM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I browsed through the site and did not take the time to realize who's site it was. I agree about the approach. It lacks information and forces you to learn how to find answers you need rather than force feeding it to you. When I complete the book, my next purchase will be a complete reference.


Similar Threads
Thread Thread Starter Forum Replies Last Post
TryIt 32 zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 5 October 13th, 2011 11:33 AM
Lesson 36 Tryit Mark P. BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 4 October 11th, 2011 02:49 PM
Chapter 24 Tryit objects not showing up Mark P. BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 6 June 24th, 2011 02:15 AM
Chapter 10 Tryit Mark P. BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 June 20th, 2011 08:20 PM
chapter 11 figure 11-7 relative positioning pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 November 29th, 2007 06:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.