53.5k views
4 votes
Write a program that demonstrates the skills we've learned throughout this quarter. This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below. You can write a simple game (e.g., tic-tac-toe, Battleship), a simulation (e.g., a zero-dimensional energy balance model of the climate), or an application (e.g., a mortgage calculator). Your main requirement is that you demonstrate six of the following features in your software program and that you comment and document your code well:

User Darlenis
by
4.8k points

1 Answer

3 votes

This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below.

The code given below is tic-tac-toe

Step-by-step explanation:

import java.util.Arrays;

import java.util.InputMismatchException;

import java.util.Scanner;

public class TicTacToe {

static Scanner in;

static String [ ] board;

static String turn;

public static void main(String[] args) {

in = new Scanner(System.in);

board = new String[9];

turn = "X";

String winner = null;

populateEmptyBoard();

System.out.println("Welcome to 2 Player Tic Tac Toe.");

System.out.println("");

printBoard();

System.out.println("X's will play first. Enter a slot number to place X in:");

while (winner == null) {

int numInput;

try {

numInput = in.nextInt();

if (!(numInput > 0 && numInput <= 9)) {

System.out.println("Invalid input; re-enter slot number:");

continue;

}

} catch (InputMismatchException e) {

System.out.println("Invalid input; re-enter slot number:");

continue;

}

if (board[numInput-1].equals(String.valueOf(numInput))) {

board[numInput-1] = turn;

if (turn.equals("X")) {

turn = "O";

} else {

turn = "X";

}

printBoard();

winner = checkWinner();

} else {

System.out.println("Slot already taken; re-enter slot number:");

continue;

}

}

if (winner.equalsIgnoreCase("draw")) {

System.out.println("It's a draw! Thanks for playing.");

} else {

System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");

}

}

static String checkWinner() {

for (int a = 0; a < 8; a++) {

String line = null;

switch (a) {

case 0:

line = board[0] + board[1] + board[2];

break;

case 1:

line = board[3] + board[4] + board[5];

break;

case 2:

line = board[6] + board[7] + board[8];

break;

case 3:

line = board[0] + board[3] + board[6];

break;

case 4:

line = board[1] + board[4] + board[7];

break;

case 5:

line = board[2] + board[5] + board[8];

break;

case 6:

line = board[0] + board[4] + board[8];

break;

case 7:

line = board[2] + board[4] + board[6];

break;

}

if (line.equals("XXX")) {

return "X";

} else if (line.equals("OOO")) {

return "O";

}

}

for (int a = 0; a < 9; a++) {

if (Arrays.asList(board).contains(String.valueOf(a+1))) {

break;

}

else if (a == 8) return "draw";

}

System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:");

return null;

}

static void printBoard() ");

System.out.println("

static void populateEmptyBoard() {

for (int a = 0; a < 9; a++) {

board[a] = String.valueOf(a+1);

}

}

}

User Akash Nil
by
5.0k points