Skip to content

Commit

Permalink
WebBeans SE Number Guess Swing Example, initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroyle authored and peteroyle committed Apr 10, 2009
1 parent bf878f6 commit 7ec2954
Show file tree
Hide file tree
Showing 10 changed files with 689 additions and 0 deletions.
107 changes: 107 additions & 0 deletions se/number-guess/pom.xml
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>org.jboss.webbeans</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.jboss.webbeans</groupId>
<artifactId>number-guess-se</artifactId>
<packaging>jar</packaging>
<name>Number Guess SE</name>
<url>http://maven.apache.org</url>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jalopy-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<!-- This wipes out UNDO in IDEs. Run manually instead.
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
-->
</plugin>
<plugin>
<groupId>com.google.code.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<basedir>${basedir}</basedir>
<header>${basedir}/src/etc/header.txt</header>
<quiet>false</quiet>
<failIfMissing>true</failIfMissing>
<aggregate>false</aggregate>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>repository.jboss.org</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.org/maven2</url>
<releases>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots.jboss.org</id>
<name>JBoss Snapshots Repository</name>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>repository.codehaus.org</id>
<name>Codehaus Repository</name>
<url>http://repository.codehaus.org</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.webbeans</groupId>
<artifactId>se-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
14 changes: 14 additions & 0 deletions se/number-guess/src/etc/header.txt
@@ -0,0 +1,14 @@
JBoss, Home of Professional Open Source
Copyright 2008, Red Hat Middleware LLC, and individual contributors
by the @authors tag. See the copyright.txt in the distribution for a
full listing of individual contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@@ -0,0 +1,141 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.webbeans.environment.se.example.numberguess;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.context.ApplicationScoped;
import javax.inject.Current;

/**
* This class contains the pure logic of the Number Guess game.
* On initialization a random number will be selected. Multiple "guesses"
* as to what that number might be can be made using the 'check' method. The
* user wins if they can guess the selected number in the alloted amount of
* tries.
*
* @author Peter Royle
*/
@ApplicationScoped
public class Game
implements Serializable
{
public static final int MAX_NUM_GUESSES = 10;

private Integer number;
private int guess = 0;
private int smallest = 0;
@MaxNumber private int maxNumber;
private int biggest;
private int remainingGuesses = MAX_NUM_GUESSES;
private boolean validNumberRange = true;

@Current Generator rndGenerator;

public Game()
{
}

public int getNumber()
{
return number;
}

public int getGuess()
{
return guess;
}

public void setGuess( int guess )
{
this.guess = guess;
}

public int getSmallest()
{
return smallest;
}

public int getBiggest()
{
return biggest;
}

public int getRemainingGuesses()
{
return remainingGuesses;
}

public boolean isValidNumberRange()
{
return validNumberRange;
}

public boolean isGameWon()
{
return guess == number;
}

public boolean isGameLost()
{
return guess != number && remainingGuesses <= 0;
}

public boolean check()
{
boolean result = false;

if ( checkNewNumberRangeIsValid() )
{
if ( guess > number )
{
biggest = guess - 1;
}

if ( guess < number )
{
smallest = guess + 1;
}

if ( guess == number )
{
result = true;
}

remainingGuesses--;
}

return result;
}

private boolean checkNewNumberRangeIsValid()
{
return validNumberRange = ( ( guess >= smallest ) && ( guess <= biggest ) );
}

@PostConstruct
public void reset()
{
this.smallest = 0;
this.guess = 0;
this.remainingGuesses = 10;
this.biggest = maxNumber;
this.number = rndGenerator.next();
System.out.println( "psst! the number is " + this.number );
}
}
@@ -0,0 +1,48 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.webbeans.environment.se.example.numberguess;

import java.io.Serializable;

import javax.context.ApplicationScoped;
import javax.inject.Produces;

@ApplicationScoped
public class Generator
implements Serializable
{
private static final long serialVersionUID = -7213673465118041882L;
private java.util.Random random = new java.util.Random( System.currentTimeMillis() );
private int maxNumber = 100;

java.util.Random getRandom()
{
return random;
}

@Produces @Random
int next()
{
return getRandom().nextInt( maxNumber );
}

@Produces @MaxNumber
int getMaxNumber()
{
return maxNumber;
}
}
@@ -0,0 +1,35 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.webbeans.environment.se.example.numberguess;

import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

import javax.inject.BindingType;
@Target( {TYPE, METHOD, PARAMETER, FIELD} )
@Retention( RUNTIME )
@Documented
@BindingType
public @interface MaxNumber
{
}

0 comments on commit 7ec2954

Please sign in to comment.