├── CODEOWNERS └── Project 1 - TicTacToe ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── java │ └── com │ │ └── udacity │ │ ├── Game.java │ │ └── GameUI.java └── resources │ ├── grid.png │ ├── o.png │ └── x.png └── test └── java └── com └── udacity └── GameTest.java /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @udacity/active-public-content -------------------------------------------------------------------------------- /Project 1 - TicTacToe/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | mainClassName = "com.udacity.Game" 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | testCompile group: 'junit', name: 'junit', version: '4.+' 12 | } 13 | 14 | tasks.withType(Test) { 15 | testLogging { 16 | // set options for log level LIFECYCLE 17 | events "passed", "skipped", "failed", "standardOut" 18 | showExceptions true 19 | exceptionFormat "short" 20 | showCauses true 21 | showStackTraces true 22 | 23 | // set options for log level DEBUG and INFO 24 | debug { 25 | events "started", "passed", "skipped", "failed", "standardOut", "standardError" 26 | exceptionFormat "full" 27 | } 28 | info.events = debug.events 29 | info.exceptionFormat = debug.exceptionFormat 30 | 31 | afterSuite { desc, result -> 32 | if (!desc.parent) { // will match the outermost suite 33 | def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)" 34 | def startItem = '| ', endItem = ' |' 35 | def repeatLength = startItem.length() + output.length() + endItem.length() 36 | println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength)) 37 | } 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /Project 1 - TicTacToe/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/ud282/5b0cbadf076c01393760141bb5fd6d8d38ffab21/Project 1 - TicTacToe/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Project 1 - TicTacToe/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Jun 21 15:48:18 PDT 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip 7 | -------------------------------------------------------------------------------- /Project 1 - TicTacToe/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /Project 1 - TicTacToe/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /Project 1 - TicTacToe/src/main/java/com/udacity/Game.java: -------------------------------------------------------------------------------- 1 | package com.udacity; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by udacity 2016 7 | * The Main class containing game logic and backend 2D array 8 | */ 9 | public class Game { 10 | 11 | private char turn; // who's turn is it, 'x' or 'o' ? x always starts 12 | private boolean twoPlayer; // true if this is a 2 player game, false if AI playing 13 | private char [][] grid; // a 2D array of chars representing the game grid 14 | private int freeSpots; // counts the number of empty spots remaining on the board (starts from 9 and counts down) 15 | private static GameUI gui; 16 | 17 | /** 18 | * Create a new single player game 19 | * 20 | */ 21 | public Game() { 22 | newGame(false); 23 | } 24 | 25 | /** 26 | * Create a new game by clearing the 2D grid and restarting the freeSpots counter and setting the turn to x 27 | * @param twoPlayer: true if this is a 2 player game, false if playing against the computer 28 | */ 29 | public void newGame(boolean twoPlayer){ 30 | //sets a game to one or two player 31 | this.twoPlayer = twoPlayer; 32 | 33 | // initialize all chars in 3x3 game grid to '-' 34 | grid = new char[3][3]; 35 | //fill all empty slots with - 36 | for (int i=0; i<3; i++){ 37 | for (int j=0; j<3; j++){ 38 | grid[i][j] = '-'; 39 | } 40 | } 41 | //start with 9 free spots and decrement by one every time a spot is taken 42 | freeSpots = 9; 43 | //x always starts 44 | turn = 'x'; 45 | } 46 | 47 | 48 | /** 49 | * Gets the char value at that particular position in the grid array 50 | * @param i the x index of the 2D array grid 51 | * @param j the y index of the 2D array grid 52 | * @return the char value at the position (i,j): 53 | * 'x' if x has played here 54 | * 'o' if o has played here 55 | * '-' if no one has played here 56 | * '!' if i or j is out of bounds 57 | */ 58 | public char gridAt(int i, int j){ 59 | if(i>=3||j>=3||i<0||j<0) 60 | return '!'; 61 | return grid[i][j]; 62 | } 63 | 64 | /** 65 | * Places current player's char at position (i,j) 66 | * Uses the variable turn to decide what char to use 67 | * @param i the x index of the 2D array grid 68 | * @param j the y index of the 2D array grid 69 | * @return boolean: true if play was successful, false if invalid play 70 | */ 71 | public boolean playAt(int i, int j){ 72 | //check for index boundries 73 | if(i>=3||j>=3||i<0||j<0) 74 | return false; 75 | //check if this position is available 76 | if(grid[i][j] != '-'){ 77 | return false; //bail out if not available 78 | } 79 | //update grid with new play based on who's turn it is 80 | grid[i][j] = turn; 81 | //update free spots 82 | freeSpots--; 83 | return true; 84 | } 85 | 86 | 87 | /** 88 | * Override 89 | * @return string format for 2D array values 90 | */ 91 | public String toString(){ 92 | return Arrays.deepToString(this.grid); 93 | } 94 | 95 | /** 96 | * Performs the winner chack and displayes a message if game is over 97 | * @return true if game is over to start a new game 98 | */ 99 | public boolean doChecks() { 100 | //check if there's a winner or tie ? 101 | String winnerMessage = checkGameWinner(grid); 102 | if (!winnerMessage.equals("None")) { 103 | gui.gameOver(winnerMessage); 104 | newGame(false); 105 | return true; 106 | } 107 | return false; 108 | } 109 | 110 | /** 111 | * Allows computer to play in a single player game or switch turns for 2 player game 112 | */ 113 | public void nextTurn(){ 114 | //check if single player game, then let computer play turn 115 | if(!twoPlayer){ 116 | if(freeSpots == 0){ 117 | return ; //bail out if no more free spots 118 | } 119 | int ai_i, ai_j; 120 | do { 121 | //randomly pick a position (ai_i,ai_j) 122 | ai_i = (int) (Math.random() * 3); 123 | ai_j = (int) (Math.random() * 3); 124 | }while(grid[ai_i][ai_j] != '-'); //keep trying if this spot was taken 125 | //update grid with new play, computer is always o 126 | grid[ai_i][ai_j] = 'o'; 127 | //update free spots 128 | freeSpots--; 129 | } 130 | else{ 131 | //change turns 132 | if(turn == 'x'){ 133 | turn = 'o'; 134 | } 135 | else{ 136 | turn = 'x'; 137 | } 138 | } 139 | return; 140 | } 141 | 142 | 143 | /** 144 | * Checks if the game has ended either because a player has won, or if the game has ended as a tie. 145 | * If game hasn't ended the return string has to be "None", 146 | * If the game ends as tie, the return string has to be "Tie", 147 | * If the game ends because there's a winner, it should return "X wins" or "O wins" accordingly 148 | * @param grid 2D array of characters representing the game board 149 | * @return String indicating the outcome of the game: "X wins" or "O wins" or "Tie" or "None" 150 | */ 151 | public String checkGameWinner(char [][]grid){ 152 | String result = "None"; 153 | //Student code goes here ... 154 | return result; 155 | } 156 | 157 | /** 158 | * Main function 159 | * @param args command line arguments 160 | */ 161 | public static void main(String args[]){ 162 | Game game = new Game(); 163 | gui = new GameUI(game); 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /Project 1 - TicTacToe/src/main/java/com/udacity/GameUI.java: -------------------------------------------------------------------------------- 1 | package com.udacity; 2 | 3 | import javax.imageio.ImageIO; 4 | import javax.swing.*; 5 | import java.awt.*; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.MouseAdapter; 9 | import java.awt.event.MouseEvent; 10 | import java.awt.image.BufferedImage; 11 | import java.io.IOException; 12 | 13 | /** 14 | * Created by udacity 2016 15 | * The Main UI class containing game logic and backend 2D array 16 | */ 17 | public class GameUI extends JPanel { 18 | 19 | //object reference to the actual game to add plays and reflect plays in UI 20 | private Game game; 21 | //frame to display grid and plays 22 | private JFrame frame; 23 | //images 24 | private BufferedImage grid; 25 | private BufferedImage x; 26 | private BufferedImage o; 27 | 28 | /** 29 | * Creates a gameUI given a Game object 30 | * @param game 31 | */ 32 | public GameUI(Game game) { 33 | 34 | this.game = game; 35 | 36 | // load images from resources files 37 | try { 38 | ClassLoader classLoader = GameUI.class.getClassLoader(); 39 | grid = ImageIO.read(classLoader.getResourceAsStream("grid.png")); 40 | x = ImageIO.read(classLoader.getResourceAsStream("x.png")); 41 | o = ImageIO.read(classLoader.getResourceAsStream("o.png")); 42 | } catch (IOException ex) { 43 | System.out.println("Failed to load images"); 44 | } 45 | 46 | //create new game buttons 47 | JButton newGameButton = new JButton("New Single Player Game"); 48 | newGameButton.setAlignmentX(Component.CENTER_ALIGNMENT); 49 | newGameButton.addActionListener(new ActionListener() { // connects the new game button to its buttonPressed method 50 | public void actionPerformed(ActionEvent e) 51 | { 52 | newGameButtonPressed(false); 53 | } 54 | }); 55 | 56 | JButton new2PlayerGameButton = new JButton("New 2 Player Game"); 57 | new2PlayerGameButton.setAlignmentX(Component.CENTER_ALIGNMENT); 58 | new2PlayerGameButton.addActionListener(new ActionListener() { // connects the new game button to its buttonPressed method 59 | public void actionPerformed(ActionEvent e) 60 | { 61 | newGameButtonPressed(true); 62 | } 63 | }); 64 | 65 | // control what happens when new game buttons cickes 66 | this.addMouseListener(new MouseAdapter() { 67 | @Override 68 | public void mouseClicked(MouseEvent e) { 69 | 70 | //get grid dimensions and location relative to mouse click 71 | JPanel panel = (JPanel)e.getSource(); 72 | double boardWidth = panel.getWidth(); 73 | double boardHeight = panel.getHeight(); 74 | double boardX = 10; // the horizontal gap left of the grid 75 | double boardY = 40; // the vertical gap above the grid 76 | //return if clicked outside the grid area 77 | if(e.getX()=0;j--) { 118 | if(game.gridAt(i,j)=='x'){ 119 | //based on grid index, calculate the pixel location to draw the x image 120 | g.drawImage(x, 200*i+40,200*j+70, null); // draw an x 121 | } 122 | else if(game.gridAt(i,j)=='o'){ 123 | //based on grid index, calculate the pixel location to draw the o image 124 | g.drawImage(o, 200*i+40,200*j+70, null); // draw an o 125 | } 126 | } 127 | } 128 | 129 | 130 | } 131 | 132 | /** 133 | * Called when mouse is clicked within grid in a valid spot 134 | * @param i the x index of the 2D grid array 135 | * @param j the y index of the 2D grid array 136 | */ 137 | private void panelMouseClicked(int i, int j) { 138 | //apply this play at position i and j 139 | if(!game.playAt(i,j)){ 140 | //escape if play was invalid 141 | return; 142 | } 143 | //refresh the display to reflect the new play 144 | this.repaint(); 145 | //check if game ended 146 | if(game.doChecks()){ 147 | //if game over no need to continue, so return 148 | return; 149 | } 150 | //let computer take turn or switch turns for next player 151 | game.nextTurn(); 152 | //refresh the display to reflect computer's turn if played 153 | this.repaint(); 154 | //check if game ended again after computer's turn 155 | if(game.doChecks()){ 156 | //if game over no need to continue, so return 157 | return; 158 | } 159 | } 160 | 161 | public void gameOver(String message){ 162 | JOptionPane.showMessageDialog(null, message, "Game Over!", JOptionPane.INFORMATION_MESSAGE); 163 | } 164 | 165 | 166 | /** 167 | * Called when onw of the new game buttons is clicked 168 | * @param twoPlayer boolean: true if starting a 2 player game, false for single player game 169 | */ 170 | private void newGameButtonPressed(boolean twoPlayer) { 171 | game.newGame(twoPlayer); 172 | this.repaint(); 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /Project 1 - TicTacToe/src/main/resources/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/ud282/5b0cbadf076c01393760141bb5fd6d8d38ffab21/Project 1 - TicTacToe/src/main/resources/grid.png -------------------------------------------------------------------------------- /Project 1 - TicTacToe/src/main/resources/o.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/ud282/5b0cbadf076c01393760141bb5fd6d8d38ffab21/Project 1 - TicTacToe/src/main/resources/o.png -------------------------------------------------------------------------------- /Project 1 - TicTacToe/src/main/resources/x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/udacity/ud282/5b0cbadf076c01393760141bb5fd6d8d38ffab21/Project 1 - TicTacToe/src/main/resources/x.png -------------------------------------------------------------------------------- /Project 1 - TicTacToe/src/test/java/com/udacity/GameTest.java: -------------------------------------------------------------------------------- 1 | package com.udacity; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import static org.junit.Assert.*; 6 | /** 7 | * Created by asser on 6/6/16. 8 | */ 9 | 10 | public class GameTest { 11 | 12 | private Game game; 13 | 14 | @Before 15 | public void setUp(){ 16 | game = new Game(); 17 | } 18 | 19 | @Test 20 | public void horizontal_x_win_case1() { 21 | // Note that horizontal here is vertical in UI and vise versa 22 | char[][] grid = { {'-', 'o', 'x'}, 23 | {'o', 'x', 'x'}, 24 | {'-', 'o', 'x'}}; 25 | assertTrue("x horizontal win fail", game.checkGameWinner(grid).equalsIgnoreCase("x wins")); 26 | } 27 | 28 | @Test 29 | public void horizontal_x_win_case2() { 30 | // Note that horizontal here is vertical in UI and vise versa 31 | char[][] grid2 = { {'x', '-', '-'}, 32 | {'x', 'x', 'o'}, 33 | {'x', 'o', 'o'}}; 34 | assertTrue("x horizontal win fail", game.checkGameWinner(grid2).equalsIgnoreCase("x wins")); 35 | } 36 | @Test 37 | public void horizontal_x_win_case3() { 38 | // Note that horizontal here is vertical in UI and vise versa 39 | char [][] grid3 = { {'-','x','-'}, 40 | {'o','x','o'}, 41 | {'x','x','o'}}; 42 | assertTrue( "x horizontal win fail", game.checkGameWinner(grid3).equalsIgnoreCase("x wins")); 43 | } 44 | 45 | @Test 46 | public void horizontal_o_win_case1() { 47 | // Note that horizontal here is vertical in UI and vise versa 48 | char[][] grid = { {'-', 'o', 'x'}, 49 | {'x', 'o', '-'}, 50 | {'-', 'o', 'x'}}; 51 | assertTrue("o horizontal win fail", game.checkGameWinner(grid).equalsIgnoreCase("o wins")); 52 | } 53 | 54 | @Test 55 | public void horizontal_o_win_case2() { 56 | // Note that horizontal here is vertical in UI and vise versa 57 | char[][] grid2 = { {'x', 'x', 'o'}, 58 | {'-', 'x', 'o'}, 59 | {'x', 'o', 'o'}}; 60 | assertTrue("o horizontal win fail", game.checkGameWinner(grid2).equalsIgnoreCase("o wins")); 61 | } 62 | @Test 63 | public void horizontal_o_win_case3() { 64 | // Note that horizontal here is vertical in UI and vise versa 65 | char [][] grid3 = { {'o','x','-'}, 66 | {'o','-','x'}, 67 | {'o','x','-'}}; 68 | assertTrue("o horizontal win fail", game.checkGameWinner(grid3).equalsIgnoreCase("o wins")); 69 | } 70 | 71 | 72 | @Test 73 | public void vertical_x_win_case1() { 74 | // Note that horizontal here is vertical in UI and vise versa 75 | char[][] grid = { {'-', 'o', 'x'}, 76 | {'x', 'x', 'x'}, 77 | {'-', 'o', 'o'}}; 78 | assertTrue("x vertical win fail", game.checkGameWinner(grid).equalsIgnoreCase("x wins")); 79 | } 80 | 81 | @Test 82 | public void vertical_x_win_case2() { 83 | // Note that horizontal here is vertical in UI and vise versa 84 | char[][] grid2 = { {'x', 'o', 'o'}, 85 | {'-', '-', 'o'}, 86 | {'x', 'x', 'x'}}; 87 | assertTrue("x vertical win fail", game.checkGameWinner(grid2).equalsIgnoreCase("x wins")); 88 | } 89 | @Test 90 | public void vertical_x_win_case3() { 91 | // Note that horizontal here is vertical in UI and vise versa 92 | char [][] grid3 = { {'x','x','x'}, 93 | {'o','-','o'}, 94 | {'x','-','o'}}; 95 | assertTrue("x vertical win fail", game.checkGameWinner(grid3).equalsIgnoreCase("x wins")); 96 | } 97 | 98 | @Test 99 | public void vertical_o_win_case1() { 100 | // Note that horizontal here is vertical in UI and vise versa 101 | char[][] grid = { {'o', 'o', 'o'}, 102 | {'x', 'x', '-'}, 103 | {'x', 'o', 'x'}}; 104 | assertTrue("o vertical win fail", game.checkGameWinner(grid).equalsIgnoreCase("o wins")); 105 | } 106 | 107 | @Test 108 | public void vertical_o_win_case2() { 109 | // Note that horizontal here is vertical in UI and vise versa 110 | char[][] grid2 = { {'x', 'x', 'o'}, 111 | {'o', 'o', 'o'}, 112 | {'x', 'o', 'x'}}; 113 | assertTrue("o vertical win fail", game.checkGameWinner(grid2).equalsIgnoreCase("o wins")); 114 | } 115 | @Test 116 | public void vertical_o_win_case3() { 117 | // Note that horizontal here is vertical in UI and vise versa 118 | char [][] grid3 = { {'x','x','-'}, 119 | {'x','-','x'}, 120 | {'o','o','o'}}; 121 | assertTrue("o vertical win fail", game.checkGameWinner(grid3).equalsIgnoreCase("o wins")); 122 | } 123 | 124 | 125 | @Test 126 | public void diagonal_x_win_case1() { 127 | // Note that horizontal here is vertical in UI and vise versa 128 | char[][] grid2 = { {'x', 'o', 'o'}, 129 | {'-', 'x', 'o'}, 130 | {'x', '-', 'x'}}; 131 | assertTrue( "x diagonal win fail", game.checkGameWinner(grid2).equalsIgnoreCase("x wins")); 132 | } 133 | @Test 134 | public void diagonal_x_win_case2() { 135 | // Note that horizontal here is vertical in UI and vise versa 136 | char [][] grid3 = { {'x','-','x'}, 137 | {'-','x','o'}, 138 | {'x','o','o'}}; 139 | assertTrue("x diagonal win fail", game.checkGameWinner(grid3).equalsIgnoreCase("x wins")); 140 | } 141 | 142 | @Test 143 | public void diagonal_o_win_case1() { 144 | // Note that horizontal here is vertical in UI and vise versa 145 | char[][] grid = { {'x', '-', 'o'}, 146 | {'x', 'o', 'x'}, 147 | {'o', 'o', 'x'}}; 148 | assertTrue("o diagonal win fail", game.checkGameWinner(grid).equalsIgnoreCase("o wins")); 149 | } 150 | 151 | @Test 152 | public void diagonal_o_win_case2() { 153 | // Note that horizontal here is vertical in UI and vise versa 154 | char[][] grid2 = { {'o', 'x', '-'}, 155 | {'x', 'o', 'x'}, 156 | {'x', 'o', 'o'}}; 157 | assertTrue("o diagonal win fail", game.checkGameWinner(grid2).equalsIgnoreCase("o wins")); 158 | } 159 | 160 | @Test 161 | public void tie_game_case1() { 162 | // Note that horizontal here is vertical in UI and vise versa 163 | char[][] grid2 = { {'o', 'x', 'o'}, 164 | {'x', 'o', 'x'}, 165 | {'x', 'o', 'x'}}; 166 | assertTrue("tie game failed", game.checkGameWinner(grid2).equalsIgnoreCase("tie")); 167 | } 168 | 169 | @Test 170 | public void tie_game_case2() { 171 | // Note that horizontal here is vertical in UI and vise versa 172 | char[][] grid2 = { {'o', 'x', 'o'}, 173 | {'o', 'x', 'x'}, 174 | {'x', 'o', 'x'}}; 175 | assertTrue("tie game failed", game.checkGameWinner(grid2).equalsIgnoreCase("tie")); 176 | } 177 | 178 | @Test 179 | public void tie_game_case3() { 180 | // Note that horizontal here is vertical in UI and vise versa 181 | char[][] grid2 = { {'x', 'o', 'o'}, 182 | {'o', 'x', 'x'}, 183 | {'x', 'o', 'o'}}; 184 | assertTrue( "tie game failed", game.checkGameWinner(grid2).equalsIgnoreCase("tie")); 185 | } 186 | 187 | 188 | @Test 189 | public void no_winner_case1() { 190 | // Note that horizontal here is vertical in UI and vise versa 191 | char[][] grid2 = { {'o', 'x', '-'}, 192 | {'o', 'x', 'x'}, 193 | {'x', 'o', 'x'}}; 194 | assertTrue("game no supposed to end", game.checkGameWinner(grid2).equalsIgnoreCase("none")); 195 | } 196 | 197 | @Test 198 | public void no_winner_case2() { 199 | // Note that horizontal here is vertical in UI and vise versa 200 | char[][] grid2 = { {'o', 'x', '-'}, 201 | {'o', 'x', 'x'}, 202 | {'-', 'o', 'x'}}; 203 | assertTrue("game no supposed to end", game.checkGameWinner(grid2).equalsIgnoreCase("none")); 204 | } 205 | 206 | @Test 207 | public void no_winner_case3() { 208 | // Note that horizontal here is vertical in UI and vise versa 209 | char[][] grid2 = { {'-', '-', '-'}, 210 | {'-', 'x', 'o'}, 211 | {'-', '-', 'x'}}; 212 | assertTrue("game no supposed to end", game.checkGameWinner(grid2).equalsIgnoreCase("none")); 213 | } 214 | 215 | @Test 216 | public void no_winner_case4() { 217 | // Note that horizontal here is vertical in UI and vise versa 218 | char[][] grid2 = { {'-', '-', '-'}, 219 | {'-', 'x', '-'}, 220 | {'-', '-', '-'}}; 221 | assertTrue("game no supposed to end", game.checkGameWinner(grid2).equalsIgnoreCase("none")); 222 | } 223 | } 224 | --------------------------------------------------------------------------------