File: SittingDuck.java

package info (click to toggle)
robocode 1.6.0~beta2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 8,456 kB
  • ctags: 4,987
  • sloc: java: 29,355; xml: 368; sh: 150; makefile: 61
file content (86 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2001, 2008 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://robocode.sourceforge.net/license/cpl-v10.html
 *
 * Contributors:
 *     Mathew A. Nelson
 *     - Initial implementation
 *******************************************************************************/
package sample;


import robocode.AdvancedRobot;
import robocode.RobocodeFileOutputStream;

import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;


/**
 * SittingDuck - a sample robot by Mathew Nelson
 * <p/>
 * Along with sitting still doing nothing,
 * this robot demonstrates persistency.
 */
public class SittingDuck extends AdvancedRobot {
	static boolean incrementedBattles = false;

	public void run() {
		setBodyColor(Color.yellow);
		setGunColor(Color.yellow);

		int roundCount, battleCount;

		// Read file "count.dat" which contains 2 lines,
		// a round count, and a battle count
		try {
			BufferedReader r = new BufferedReader(new FileReader(getDataFile("count.dat")));

			// Try to get the counts
			roundCount = Integer.parseInt(r.readLine());
			battleCount = Integer.parseInt(r.readLine());
		} catch (IOException e) {
			// Something went wrong reading the file, reset to 0.
			roundCount = 0;
			battleCount = 0;
		} catch (NumberFormatException e) {
			// Something went wrong converting to ints, reset to 0
			roundCount = 0;
			battleCount = 0;
		}

		// Increment the # of rounds
		roundCount++;
		// If we havenn't incremented # of battles already,
		// (Note:  Because robots are only instantiated once per battle,
		// member variables remain valid throughout it.
		if (!incrementedBattles) {
			// Increment # of battles
			battleCount++;
			incrementedBattles = true;
		}

		try {
			PrintStream w = new PrintStream(new RobocodeFileOutputStream(getDataFile("count.dat")));

			w.println(roundCount);
			w.println(battleCount);
			// PrintStreams don't throw IOExceptions during prints,
			// they simply set a flag.... so check it here.
			if (w.checkError()) {
				out.println("I could not write the count!");
			}
			w.close();
		} catch (IOException e) {
			out.println("IOException trying to write: " + e);
		}

		out.println("I have been a sitting duck for " + roundCount + " rounds, in " + battleCount + " battles.");
	}
}