CSCI A201/A597

Lab Notes 14

Spring 2000


More solutions to the practice problems listed for the practical.

Sample Problem 2.

Write a program that defines, allocates and initializes a two dimensional array of integers. Then find the maximum among the array elements. Display the array as a grid of gray squares where for each cell in the grid the intensity of the color is the ratio between the number in the cell and the max value in the array. Check out 4.4.1. in your text for color functions.
Here's a solution:
import element.*;
import java.awt.Color; 

public class ColorMap {
    public static void main(String[] args) {
	int lines = Integer.parseInt(args[0]),
	    columns = Integer.parseInt(args[1]); 
	int[][] map = new int[lines][columns]; 
	Library.initialize(map); 
	int max = Library.findMax(map); 
	Library.display(map, max); 
    } 
} 

class Library {
    public static void initialize(int[][] m) {
	for (int i = 0; i < m.length; i++) {
	    for (int j = 0; j < m[i].length; j++) {
		m[i][j] = (int)(Math.random() * 200); 
	    } 
	} 
    } 
    public static int findMax(int[][] m) {
	int max = m[0][0];
	for (int i = 0; i < m.length; i++) {
	    for (int j = 0; j < m[i].length; j++) {
		if (max < m[i][j]) {
		    max = m[i][j]; 
		} 
	    }
	}
	return max; 
    } 
    public static void display(int[][] m, int max) {
	DrawingWindow d = new DrawingWindow(); 
	int xL = 10, yL = 10, w = 10, h = 10; 
	for (int i = 0; i < m.length; i++) {
	    for (int j = 0; j < m[i].length; j++) {
		int value = (int) (255.0 * m[i][j] / max); 
		Color c = new Color(value, value, value); 
		d.setForeground(c); 
		Rect cell = new Rect(xL + j * w, yL + i * h, w, h); 
		d.fill(cell); 
	    } 
	} 
    } 
} 
You need to pass the size of the grid on the command line.

What would it take to make the size of the cells adjustable by the user too?


Sample Problem 14.

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 x 3 grid as in

The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, accept mouse clicks into empty squares, change the players after every successful move, and pronounce the winner (or the loser for that matter).

Here's a solution to it.

import element.*;

public class TicTacToe {
    public static void main(String[] args) {
	char[][] grid = {{' ', ' ', ' '},
			 {' ', ' ', ' '},
			 {' ', ' ', ' '}}; 
	Rect[][] g = new Rect[3][3]; 
	int xL = 30, yL = 30, w = 30, h = 30; 
	DrawingWindow d = new DrawingWindow(); 
	for (int i = 0; i < g.length; i++) {
	    for (int j = 0; j < g[i].length; j++) {
		Rect cell = new Rect(xL + j * w, yL + i * h, w, h); 
		g[i][j] = cell; 
		d.draw(cell); 
	    } 
	} 
	boolean gameOn = true; 
	char player = 'x'; 
	while (gameOn) {
	    Pt m = d.awaitMouseClick(); 
	    int line, column; 
	    line = -1; column = -1; 
	    for (int i = 0; i < g.length; i++) {
		for (int j = 0; j < g[i].length; j++) {
		    if (g[i][j].contains(m)) {
			line = i; 
			column = j; 
		    } 
		} 
	    }
	    System.out.println(line + " " + column); 
	    if (line >= 0 && column >= 0 && grid[line][column] == ' ') {
		grid[line][column] = player; 
		Text t = new Text(player); 
		t.center(g[line][column].center()); 
		d.draw(t); 
		System.out.println("Drawing " + line + " " + column); 
		if (player == 'x') {
		    player = '0'; 
		} else { 
		    player = 'x'; 
		} 
	    }
	    gameOn = (Library.check(grid) == false); 
	}
	System.out.println("Player " + player + " lost."); 
    }
}

class Library {
    public static boolean check (char[][] g) {
	return ((g[0][0] == g[0][1] && g[0][1] == g[0][2] && g[0][0] != ' ') ||
                (g[1][0] == g[1][1] && g[1][1] == g[1][2] && g[1][0] != ' ') ||
                (g[2][0] == g[2][1] && g[2][1] == g[2][2] && g[2][0] != ' ') ||

                (g[0][0] == g[1][0] && g[1][0] == g[2][0] && g[0][0] != ' ') ||
                (g[0][1] == g[1][1] && g[1][1] == g[2][1] && g[0][1] != ' ') ||
                (g[0][2] == g[1][2] && g[2][2] == g[2][2] && g[0][2] != ' ') ||

                (g[0][0] == g[1][1] && g[1][1] == g[2][2] && g[0][0] != ' ') ||
                (g[2][0] == g[1][1] && g[1][1] == g[2][0] && g[2][0] != ' ')); 
             /* There's a mistake in the line above and Brian Bell found it yesterday
                in class! The code for the second diagonal condition is incorrect in that
                the last cell on the diagonal has the indices reversed. Thanks, Brian
                and I'll just mark the incorrect indices and let you fix them on your own 
                when you try this on your computers. 
              */ 
    } 
}

Other Sample Problems.


Last updated Apr 20, 2000 by Adrian German.