Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FaultInOurPong
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Group3
FaultInOurPong
Commits
417d19e6
Commit
417d19e6
authored
8 years ago
by
Adwity Sharma
Browse files
Options
Downloads
Patches
Plain Diff
This file adds the user info to the high score text file if the user made it to high score.
parent
22d9a5aa
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/model/HighScore.java
+174
-0
174 additions, 0 deletions
src/model/HighScore.java
with
174 additions
and
0 deletions
src/model/HighScore.java
0 → 100644
+
174
−
0
View file @
417d19e6
import
java.awt.List
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.PrintWriter
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Scanner
;
public
class
HighScore
{
ArrayList
<
String
>
arylist
=
new
ArrayList
<
String
>();
static
ArrayList
<
String
>
user
=
new
ArrayList
<
String
>();
static
ArrayList
<
String
>
score
=
new
ArrayList
<
String
>();
static
ArrayList
<
Integer
>
scoreInt
=
new
ArrayList
<
Integer
>();
static
int
x
;
public
static
String
[]
readFrom
()
throws
IOException
{
String
fileName
=
"highScore.txt"
;
String
content
=
null
;
File
file
=
new
File
(
fileName
);
FileReader
reader
=
null
;
try
{
reader
=
new
FileReader
(
file
);
char
[]
chars
=
new
char
[(
int
)
file
.
length
()];
reader
.
read
(
chars
);
content
=
new
String
(
chars
);
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
reader
!=
null
)
{
reader
.
close
();
}
}
String
[]
words
=
content
.
split
(
"\\s+"
);
// for (int i = 0; i < words.length; i++) {
// System.out.println(words[i]);
// }
return
words
;
}
public
static
void
writeTo
()
{
try
{
String
filename
=
"highScore.txt"
;
FileWriter
fw
=
new
FileWriter
(
filename
,
true
);
PrintWriter
pw
=
new
PrintWriter
(
filename
);
pw
.
print
(
" "
);
for
(
int
i
=
0
;
i
<
user
.
size
();
i
++){
fw
.
write
(
user
.
get
(
i
)+
" "
+
scoreInt
.
get
(
i
)
+
"\n"
);
}
// fw.write(a + "\n");
fw
.
close
();
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"IOException: "
+
ioe
.
getMessage
());
}
}
public
static
void
creatingArrays
()
throws
IOException
{
String
[]
newArr
=
readFrom
();
for
(
int
i
=
0
;
i
<
newArr
.
length
;
i
++)
{
if
(
i
%
2
==
0
)
{
user
.
add
(
newArr
[
i
]);
}
if
(
i
%
2
==
1
)
{
score
.
add
(
newArr
[
i
]);
}
}
// for(int i = 0; i < user.size(); i++){
// System.out.println(user.get(i));
//
// }
//
// for(int i = 0; i < score.size(); i++){
// System.out.println(score.get(i));
//
// }
for
(
int
i
=
0
;
i
<
score
.
size
();
i
++)
{
scoreInt
.
add
(
Integer
.
parseInt
(
score
.
get
(
i
)));
}
// for (int i = 0; i < score.size(); i++) {
// System.out.println(scoreInt.get(i));
//
// }
}
public
int
returnLowest
()
{
this
.
x
=
scoreInt
.
get
(
scoreInt
.
size
());
return
scoreInt
.
get
(
scoreInt
.
size
());
}
public
static
boolean
isHigh
(
int
compare
)
{
if
(
compare
>
x
)
{
return
true
;
}
else
{
return
false
;
}
}
public
static
void
sortInt
()
{
for
(
int
i
=
0
;
i
<
scoreInt
.
size
();
i
++)
{
for
(
int
j
=
0
;
j
<
scoreInt
.
size
()
-
1
;
j
++)
{
if
(
scoreInt
.
get
(
j
)
<
scoreInt
.
get
(
j
+
1
))
{
int
temp
=
0
;
String
tempS
=
""
;
temp
=
scoreInt
.
get
(
j
);
tempS
=
user
.
get
(
j
);
scoreInt
.
set
(
j
,
scoreInt
.
get
(
j
+
1
));
user
.
set
(
j
,
user
.
get
(
j
+
1
));
scoreInt
.
set
(
j
+
1
,
temp
);
user
.
set
(
j
+
1
,
tempS
);
}
}
}
for
(
int
i
=
0
;
i
<
scoreInt
.
size
();
i
++)
{
System
.
out
.
println
(
user
.
get
(
i
)
+
" "
+
scoreInt
.
get
(
i
));
}
}
public
HighScore
(
String
nameUser
,
int
nameScore
)
throws
IOException
{
readFrom
();
creatingArrays
();
sortInt
();
String
name
=
nameUser
;
int
nameScored
=
nameScore
;
if
(
isHigh
(
nameScored
)){
user
.
add
(
name
);
scoreInt
.
add
(
nameScored
);
sortInt
();
user
.
remove
(
user
.
size
()-
1
);
scoreInt
.
remove
(
scoreInt
.
size
()-
1
);
writeTo
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment