Tic-Tac-Toe, it's three-in-a-row! With Tic-Tac-Toe, and unlike Connect Four, you do not have to worry about gravity.
Question:
Tic-Tac-Toe, it's three-in-a-row!
With Tic-Tac-Toe, and unlike Connect Four, you do not have to worry about gravity. A player can place a token in any row or column that is unoccupied. The board is a33 array in which players need to place three of their tokens in a row to win. With some modifications to the Connect Four program, you'll be able to make a fully functional Tic-Tac-Toe game.
Write java source code and pseudo code
Connect Four program.
/* * ConnectFour is a 2-D array game. There are 4 rows and 4 columns the players have to choose from. If a player has 4 tokens in a row (horizontal, vertical or diagonal) then they win. * 0 1 2 3 * 0 _ _ _ _ * 1 _ _ _ _ * 2 _ _ _ _ * 3 _ _ _ _ * The player selects the column they wish to drop their piece into, if a player wins the game alerts the users and asks if they wish to reset the board for another game. Author: Mike King, 2013 */
import java.util.Scanner;
public class ConnectFour {
static char[][] board = new char[4][4]; //playable board area static boolean gameOver = false; // gameOver indicates if a player won a game static boolean exit = false; // exit indicates if there are no other games to play and to exit the program static char player1 = 'a'; // player 1's token is a static char player2 = 'b'; // player 2's token is b static char turn = player1; // turn specifies which player's turn it is public static void main(String[] args) { while (!exit){ //while exit has not been set to true System.out.println("Connect Four! Player " + turn + "\'s turn!"); printBoard(); //print the boards state while (!gameOver){ //while the game is not over, execute turns move(turn); } } } /* * Move places a piece in a column, if there's room for it. */ private static boolean move(char p){ Scanner s = new Scanner(System.in); String str; int c = 0; boolean playable = false; //Get the column choice from the user while (playable == false) { System.out.println("It's player " + turn + "\'s turn! Which column do you want to lay a piece in? 0, 1, 2, 3"); str = s.nextLine(); c = Character.getNumericValue(str.charAt(0)); //If the column choice is invalid, get a valid one if (c != 0 && c != 1 && c != 2 && c != 3){ System.out.println("Sorry that wasn't a valid option. You entered \"" + c + "\" Please enter either 0, 1, 2 or 3"); playable = false; } else { playable = checkCol(c); //if the column is full, then select another one if (playable == false){ System.out.println("Sorry! Column " + c + " is full!"); } } } //If the column has a spot for your token, play it if (playable) { int i = 3; int free; char choice;
//while loop determines the row the free slot is while (board[c][i] == player1 || board[c][i] == player2){ i--; } board[c][i] = p; //place piece in free slot System.out.println(" "); printBoard(); //check if the player has won the game if (checkCharForWin(p)){ System.out.println("Player " + p + " WINS! Reset Game? y or n"); gameOver = true; str = s.nextLine(); choice = str.charAt(0); //check the user's input if they wish to continue playing if (choice != 'y' && choice !='n' ){ System.out.println("Sorry, invalid input. Please enter \'y\' or \'n\'"); str = s.nextLine(); choice = str.charAt(0); } //if the user wishes to continue playing, reset the game if (choice == 'y'){ clearGame(); System.out.println(" "); printBoard(); } else { //else quit the program System.out.println("Goodbye!"); exit = true; } } else if (checkDraw()){ System.out.println("Draw! Reset game? y or n"); gameOver = true; str = s.nextLine(); choice = str.charAt(0); //check the user's input if they wish to continue playing if (choice != 'y' && choice !='n' ){ System.out.println("Sorry, invalid input. Please enter \'y\' or \'n\'"); str = s.nextLine(); choice = str.charAt(0); } //if the user wishes to continue playing, reset the game if (choice == 'y'){ clearGame(); System.out.println(" "); printBoard(); } else { //else quit the program System.out.println("Goodbye!"); exit = true; } } //change player's turn, only if they successfully moved if (turn == player1){ turn = player2; } else { turn = player1; } }
return playable; } /* * checkCharForWin checks the board to see if the passed character has a winning position */ private static boolean checkCharForWin(char x){ boolean win = false; for (int i = 0; i < 4; i ++){ //horizontal win if (board[i][0] == x && board[i][1] == x && board[i][2] == x && board[i][3] == x){ win = true; } //vertical win if (board[0][i] == x && board[1][i] == x && board[2][i] == x && board[3][i] == x){ win = true; } } //diagonal wins if (board[0][0] == x && board[1][1] == x && board[2][2] == x && board[3][3] == x){ win = true; } if (board[0][3] == x && board[1][2] == x && board[2][1] == x && board[3][0] == x){ win = true; }
return win; } /* * checkCol takes an integer between 0 and 2 and checks the column if there are moves available in that column */ private static boolean checkCol(int x){ boolean playable = false; int i = 0; while (playable == false && i < 4){ if (board[x][i] != player1 && board[x][i] != player2){ playable = true; } i++; } return playable; } /* * checkDraw makes sure the board is not full, if the board is full and no one has one the game is a draw */ private static boolean checkDraw(){ boolean draw = true; for (int i = 0; i < 4; i ++) { for (int j = 0; j < 4; j++){ if (board[i][j] != player1 && board[i][j] != player2){ draw = false; } } } return draw; } /* * printBoard displays the current connect 4 board */ private static void printBoard(){ System.out.println("0\t1\t2\t3"); for (int i = 0; i < 4; i++){ System.out.println(print(board[0][i]) + "\t" + print(board[1][i]) + "\t" + print(board[2][i]) + "\t" + print(board[3][i])); } } /* * print returns the player token if the character is a player token, otherwise it returns _ */ private static char print(char x){ if (x == player1 || x == player2){ return x; } else { return '_'; } } /* * clearGame resets the board to blank, returns the users their pieces */ private static void clearGame(){ for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ board[i][j] = 'x'; } } gameOver = false; }
Fundamentals of corporate finance
ISBN: 978-0470876442
2nd Edition
Authors: Robert Parrino, David S. Kidwell, Thomas W. Bates