Skip to content

Commit 8fdb99f

Browse files
committed
Simple Text Based Tank Game
1 parent 8ed9b27 commit 8fdb99f

21 files changed

+2350
-0
lines changed

tankGame/build.xml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- You may freely edit this file. See commented blocks below for -->
3+
<!-- some examples of how to customize the build. -->
4+
<!-- (If you delete it and reopen the project it will be recreated.) -->
5+
<!-- By default, only the Clean and Build commands use this build script. -->
6+
<!-- Commands such as Run, Debug, and Test only use this build script if -->
7+
<!-- the Compile on Save feature is turned off for the project. -->
8+
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
9+
<!-- in the project's Project Properties dialog box.-->
10+
<project name="tankGame" default="default" basedir=".">
11+
<description>Builds, tests, and runs the project tankGame.</description>
12+
<import file="nbproject/build-impl.xml"/>
13+
<!--
14+
15+
There exist several targets which are by default empty and which can be
16+
used for execution of your tasks. These targets are usually executed
17+
before and after some main targets. They are:
18+
19+
-pre-init: called before initialization of project properties
20+
-post-init: called after initialization of project properties
21+
-pre-compile: called before javac compilation
22+
-post-compile: called after javac compilation
23+
-pre-compile-single: called before javac compilation of single file
24+
-post-compile-single: called after javac compilation of single file
25+
-pre-compile-test: called before javac compilation of JUnit tests
26+
-post-compile-test: called after javac compilation of JUnit tests
27+
-pre-compile-test-single: called before javac compilation of single JUnit test
28+
-post-compile-test-single: called after javac compilation of single JUunit test
29+
-pre-jar: called before JAR building
30+
-post-jar: called after JAR building
31+
-post-clean: called after cleaning build products
32+
33+
(Targets beginning with '-' are not intended to be called on their own.)
34+
35+
Example of inserting an obfuscator after compilation could look like this:
36+
37+
<target name="-post-compile">
38+
<obfuscate>
39+
<fileset dir="${build.classes.dir}"/>
40+
</obfuscate>
41+
</target>
42+
43+
For list of available properties check the imported
44+
nbproject/build-impl.xml file.
45+
46+
47+
Another way to customize the build is by overriding existing main targets.
48+
The targets of interest are:
49+
50+
-init-macrodef-javac: defines macro for javac compilation
51+
-init-macrodef-junit: defines macro for junit execution
52+
-init-macrodef-debug: defines macro for class debugging
53+
-init-macrodef-java: defines macro for class execution
54+
-do-jar: JAR building
55+
run: execution of project
56+
-javadoc-build: Javadoc generation
57+
test-report: JUnit report generation
58+
59+
An example of overriding the target for project execution could look like this:
60+
61+
<target name="run" depends="tankGame-impl.jar">
62+
<exec dir="bin" executable="launcher.exe">
63+
<arg file="${dist.jar}"/>
64+
</exec>
65+
</target>
66+
67+
Notice that the overridden target depends on the jar target and not only on
68+
the compile target as the regular run target does. Again, for a list of available
69+
properties which you can use, check the target you are overriding in the
70+
nbproject/build-impl.xml file.
71+
72+
-->
73+
</project>

tankGame/build/classes/.netbeans_automatic_build

Whitespace-only changes.

tankGame/build/classes/.netbeans_update_resources

Whitespace-only changes.
252 Bytes
Binary file not shown.
2.44 KB
Binary file not shown.
3.84 KB
Binary file not shown.
Binary file not shown.
5.43 KB
Binary file not shown.
450 Bytes
Binary file not shown.

tankGame/manifest.mf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
X-COMMENT: Main-Class will be added automatically by build
3+

tankGame/nbproject/build-impl.xml

+1,407
Large diffs are not rendered by default.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build.xml.data.CRC32=8b8bfb50
2+
build.xml.script.CRC32=f885f7cb
3+
build.xml.stylesheet.CRC32=[email protected]
4+
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
5+
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6+
nbproject/build-impl.xml.data.CRC32=8b8bfb50
7+
nbproject/build-impl.xml.script.CRC32=098a80dd
8+
nbproject/build-impl.xml.stylesheet.CRC32=[email protected]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
compile.on.save=true
2+
user.properties.file=C:\\Users\\Espinas\\AppData\\Roaming\\NetBeans\\7.4\\build.properties
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3+
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
4+
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
5+
<group/>
6+
</open-files>
7+
</project-private>

tankGame/nbproject/project.properties

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
annotation.processing.enabled=true
2+
annotation.processing.enabled.in.editor=false
3+
annotation.processing.processor.options=
4+
annotation.processing.processors.list=
5+
annotation.processing.run.all.processors=true
6+
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
7+
build.classes.dir=${build.dir}/classes
8+
build.classes.excludes=**/*.java,**/*.form
9+
# This directory is removed when the project is cleaned:
10+
build.dir=build
11+
build.generated.dir=${build.dir}/generated
12+
build.generated.sources.dir=${build.dir}/generated-sources
13+
# Only compile against the classpath explicitly listed here:
14+
build.sysclasspath=ignore
15+
build.test.classes.dir=${build.dir}/test/classes
16+
build.test.results.dir=${build.dir}/test/results
17+
# Uncomment to specify the preferred debugger connection transport:
18+
#debug.transport=dt_socket
19+
debug.classpath=\
20+
${run.classpath}
21+
debug.test.classpath=\
22+
${run.test.classpath}
23+
# Files in build.classes.dir which should be excluded from distribution jar
24+
dist.archive.excludes=
25+
# This directory is removed when the project is cleaned:
26+
dist.dir=dist
27+
dist.jar=${dist.dir}/tankGame.jar
28+
dist.javadoc.dir=${dist.dir}/javadoc
29+
excludes=
30+
includes=**
31+
jar.compress=false
32+
javac.classpath=
33+
# Space-separated list of extra javac options
34+
javac.compilerargs=
35+
javac.deprecation=false
36+
javac.processorpath=\
37+
${javac.classpath}
38+
javac.source=1.7
39+
javac.target=1.7
40+
javac.test.classpath=\
41+
${javac.classpath}:\
42+
${build.classes.dir}
43+
javac.test.processorpath=\
44+
${javac.test.classpath}
45+
javadoc.additionalparam=
46+
javadoc.author=false
47+
javadoc.encoding=${source.encoding}
48+
javadoc.noindex=false
49+
javadoc.nonavbar=false
50+
javadoc.notree=false
51+
javadoc.private=false
52+
javadoc.splitindex=true
53+
javadoc.use=true
54+
javadoc.version=false
55+
javadoc.windowtitle=
56+
main.class=tankgame.TankGame
57+
manifest.file=manifest.mf
58+
meta.inf.dir=${src.dir}/META-INF
59+
mkdist.disabled=false
60+
platform.active=default_platform
61+
run.classpath=\
62+
${javac.classpath}:\
63+
${build.classes.dir}
64+
# Space-separated list of JVM arguments used when running the project.
65+
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
66+
# To set system properties for unit tests define test-sys-prop.name=value:
67+
run.jvmargs=
68+
run.test.classpath=\
69+
${javac.test.classpath}:\
70+
${build.test.classes.dir}
71+
source.encoding=UTF-8
72+
src.dir=src
73+
test.src.dir=test

tankGame/nbproject/project.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://www.netbeans.org/ns/project/1">
3+
<type>org.netbeans.modules.java.j2seproject</type>
4+
<configuration>
5+
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
6+
<name>tankGame</name>
7+
<source-roots>
8+
<root id="src.dir"/>
9+
</source-roots>
10+
<test-roots>
11+
<root id="test.src.dir"/>
12+
</test-roots>
13+
</data>
14+
</configuration>
15+
</project>

tankGame/src/tankgame/AIPlayer.java

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package tankgame;
2+
3+
/**
4+
* CPU player that uses AI
5+
*
6+
* @author Sajad Azami
7+
*/
8+
public class AIPlayer {
9+
10+
int[][] field;//0 for empty,1 for tank,2 for players target
11+
int[][] targetfield;//0 for not targeted and 1 for hit,2 for miss
12+
int lenght;
13+
int orientation;
14+
int xPos = 0, yPos = 0;
15+
16+
/**
17+
* Constructor
18+
*/
19+
public AIPlayer() {
20+
field = new int[10][10];
21+
targetfield = new int[10][10];
22+
}
23+
24+
/**
25+
* Generate a random integer between max and min
26+
*/
27+
public int generateRandomNum(int min, int max) {
28+
return min + (int) (Math.random() * ((max - min) + 1));
29+
}
30+
31+
/**
32+
* Create AIplayer's field by asking him tank position
33+
*/
34+
public void createField() {
35+
boolean isComplete, isError;
36+
boolean isForError;
37+
for (int tankCounter = 1; tankCounter <= 5; tankCounter++) {
38+
isComplete = false;
39+
isError = true;
40+
while (!isComplete || isError) {
41+
isForError = false;
42+
orientation = generateRandomNum(1, 4);
43+
lenght = generateRandomNum(2, 5);
44+
xPos = generateRandomNum(0, 9);
45+
yPos = generateRandomNum(0, 9);
46+
47+
//Vertical to right
48+
if (orientation == 1) {
49+
if (xPos + lenght - 1 <= 9) {
50+
for (int i = 1; i <= lenght - 1 && !isForError; i++) {
51+
if (field[xPos + i][yPos] != 1) {
52+
isError = false;
53+
} else {
54+
isForError = true;
55+
isError = true;
56+
}
57+
}
58+
if (!isError) {
59+
for (int j = 1; j <= lenght - 1 && !isForError; j++) {
60+
field[xPos + j][yPos] = 1;
61+
}
62+
field[xPos][yPos] = 1;
63+
}
64+
isComplete = true;
65+
} else {
66+
isComplete = false;
67+
}
68+
} //Vertical to left
69+
else if (orientation == 2) {
70+
if (xPos - (lenght - 1) >= 0) {
71+
for (int i = 1; i <= lenght - 1 && !isForError; i++) {
72+
if (field[xPos - i][yPos] != 1) {
73+
isError = false;
74+
} else {
75+
isForError = true;
76+
isError = true;
77+
}
78+
}
79+
if (!isError) {
80+
for (int j = 1; j <= lenght - 1 && !isForError; j++) {
81+
field[xPos - j][yPos] = 1;
82+
}
83+
field[xPos][yPos] = 1;
84+
}
85+
isComplete = true;
86+
} else {
87+
isComplete = false;
88+
}
89+
} //Horizontal to up
90+
else if (orientation == 3) {
91+
if (yPos + lenght - 1 <= 9) {
92+
for (int i = 1; i <= lenght - 1 && !isForError; i++) {
93+
if (field[xPos][yPos + i] != 1) {
94+
isError = false;
95+
} else {
96+
isForError = true;
97+
isError = true;
98+
}
99+
}
100+
if (!isError) {
101+
for (int j = 1; j <= lenght - 1 && !isForError; j++) {
102+
field[xPos][yPos + j] = 1;
103+
}
104+
field[xPos][yPos] = 1;
105+
}
106+
isComplete = true;
107+
} else {
108+
isComplete = false;
109+
}
110+
} //Horizontal to down
111+
else if (orientation == 4) {
112+
if (yPos - (lenght - 1) >= 0) {
113+
for (int i = 1; i <= lenght - 1 && !isForError; i++) {
114+
if (field[xPos][yPos - i] != 1) {
115+
isError = false;
116+
} else {
117+
isForError = true;
118+
isError = true;
119+
}
120+
}
121+
if (!isError) {
122+
for (int j = 1; j <= lenght - 1 && !isForError; j++) {
123+
field[xPos][yPos - j] = 1;
124+
}
125+
field[xPos][yPos] = 1;
126+
}
127+
isComplete = true;
128+
} else {
129+
isComplete = false;
130+
}
131+
}
132+
}
133+
}
134+
}
135+
136+
/**
137+
* Print the map
138+
*/
139+
public void printMap() {
140+
System.out.println("Your field:");
141+
for (int j = 9; j >= 0; j--) {
142+
for (int i = 0; i <= 9; i++) {
143+
if (field[i][j] == 1) {
144+
System.out.print("#");
145+
}
146+
if (field[i][j] == 0) {
147+
System.out.print(" ");
148+
}
149+
System.out.print(" | ");
150+
if (i == 9) {
151+
System.out.println("");
152+
}
153+
}
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)