| CSCI A201/A597Lab Notes 14 Spring 2000 |
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?
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 x 3 grid as inHere's a solution to it.
![]()
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).
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.
*/
}
}
import element.*;
import java.awt.Color;
public class Life {
public static void main(String[] args) {
int lines = Integer.parseInt(args[0]);
int columns = Integer.parseInt(args[1]);
int width = Integer.parseInt(args[2]);
int height = Integer.parseInt(args[3]);
boolean[][] grid = new boolean[lines][columns];
Library.initialize(grid);
DrawingWindow d = new DrawingWindow();
Library.display(grid, width, height, d);
int generation = 0;
while (true) {
System.out.println("Generation " + generation + " printed.");
Library.waitNSeconds(2);
grid = Library.update(grid);
Library.display(grid, width, height, d);
generation += 1;
}
}
}
class Library {
public static void initialize(boolean[][] b) {
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
double a = Math.random();
if (a > 0.5) {
b[i][j] = true;;
} else {
b[i][j] = false;
}
}
}
}
public static void display(boolean[][] b, int w, int h, DrawingWindow d) {
int xL = 20, yL = 30;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
Rect r = new Rect(xL + j * w, yL + i * h, w, h);
if (b[i][j]) {
d.setForeground(Color.black);
d.fill(r);
} else {
d.setForeground(Color.white);
d.fill(r);
}
d.setForeground(Color.black);
d.draw(r);
}
}
Rect area = new Rect(xL, yL, b.length * w, b.length * h);
d.setForeground(Color.black);
d.draw(area);
}
public static void waitNSeconds(int delay) {
long now = System.currentTimeMillis();
long then = now + 1000 * delay;
while (System.currentTimeMillis() < then) {
}
}
public static boolean[][] update(boolean[][] b) {
boolean[][] n = new boolean[b.length][b.length];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
int neighbours = 0;
for (int k = i - 1; k <= i + 1; k++) {
for (int l = j - 1; l <= j + 1; l++) {
if (k >= 0 && k < b.length &&
l >= 0 && l < b[i].length &&
!(k == i && l == j) && b[k][l]) {
neighbours += 1;
}
}
}
if (!b[i][j] && neighbours == 3) {
n[i][j] = true;
} else if (b[i][j]) {
if (neighbours == 2 || neighbours == 3) {
n[i][j] = true;
} else {
n[i][j] = false;
}
} else { n[i][j] = false; }
}
}
return n;
}
}