Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 26th, 2012, 01: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?
Reply With Quote
  #2 (permalink)  
Old February 27th, 2012, 10: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!
Reply With Quote
  #3 (permalink)  
Old February 29th, 2012, 01:32 AM
Authorized User
Points: 61, Level: 1
Points: 61, Level: 1 Points: 61, Level: 1 Points: 61, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 18
Thanks: 3
Thanked 1 Time in 1 Post
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.
Reply With Quote
  #4 (permalink)  
Old March 1st, 2012, 05:14 AM
Authorized User
Points: 61, Level: 1
Points: 61, Level: 1 Points: 61, Level: 1 Points: 61, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 18
Thanks: 3
Thanked 1 Time in 1 Post
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
Reply With Quote
  #5 (permalink)  
Old March 1st, 2012, 09: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.
Reply With Quote
  #6 (permalink)  
Old March 1st, 2012, 12:20 PM
Authorized User
Points: 61, Level: 1
Points: 61, Level: 1 Points: 61, Level: 1 Points: 61, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 18
Thanks: 3
Thanked 1 Time in 1 Post
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.
Reply With Quote
  #7 (permalink)  
Old March 2nd, 2012, 08: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.
Reply With Quote
  #8 (permalink)  
Old March 2nd, 2012, 09:13 AM
Authorized User
Points: 61, Level: 1
Points: 61, Level: 1 Points: 61, Level: 1 Points: 61, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
Posts: 18
Thanks: 3
Thanked 1 Time in 1 Post
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.
Reply With Quote
  #9 (permalink)  
Old March 3rd, 2012, 12: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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 05:11 AM



All times are GMT -4. The time now is 05:17 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.