/* * TicTacToe.java * * Author: Craige * * Created on February 19, 2007, 12:50 PM * * Comments: Chapter 3 - GPJ SE * Exercise 5: Create a TicTacToe board application that displays a tic-tac-toe * board with an x in the center. */ import java.io.*; public class TicTacToe { /** * @param args the command line arguments */ public static void main(String[] args) { char[][] marks = { {' ', ' ', ' '}, {' ', 'x', ' '}, {' ', ' ', ' '} }; dspBoard(9, marks); } public static void dspBoard(int size, char[][] marks) { int cellSize = size/3; int fullSize = size + 2; String format; String leftpad = "", rightpad = ""; // Set the padding for (int a = 0; a < (cellSize/2) - 1; a++) { leftpad = rightpad += " "; } for (int i = 1; i < fullSize; i++) { // Get the board row number (1-3) int row = (int)Math.ceil(i/cellSize); if ( i == Math.floor(cellSize/2) +1) { System.out.format("\n%s%s%s|%s%s%s|%s%s%s", leftpad, marks[row][1], rightpad, leftpad, marks[row][2], rightpad, leftpad, marks[row][3], rightpad); } else { System.out.format("\n%s%s%s|%s%s%s|%s%s%s", leftpad, ' ', rightpad, leftpad, ' ', rightpad, leftpad, ' ', rightpad); } if (( i % cellSize) == 1 ) { for (int c = 0; c < fullSize; i++) { System.out.print("-"); } } } } }