From 549dc0a399231c83e277c059e1841b10d91866ac Mon Sep 17 00:00:00 2001 From: selim Date: Sun, 28 Apr 2024 16:48:48 +0200 Subject: [PATCH] highscore set Exception handling --- bin/csv/highscores.csv | 0 bin/facade/YahtzeeGame.class | Bin 2799 -> 7829 bytes src/csv/highscores.csv | 0 src/facade/YahtzeeGame.java | 277 +++++++++++++++++++++++++++++++++-- 4 files changed, 263 insertions(+), 14 deletions(-) create mode 100644 bin/csv/highscores.csv create mode 100644 src/csv/highscores.csv diff --git a/bin/csv/highscores.csv b/bin/csv/highscores.csv new file mode 100644 index 0000000..e69de29 diff --git a/bin/facade/YahtzeeGame.class b/bin/facade/YahtzeeGame.class index ad2bd07918f7f5f56e391f7b996a63ed10e718b5..0563abcabf4754d3e198961be6b0bd9fbdc1ff17 100644 GIT binary patch literal 7829 zcmdT}3wTu3wf^^c4#NovLnaU)yaWswj7E$#2_Ua%Fa$0GSV&u)B!^^RGLz0sB3N3* zMn$c*wmu41Zi&*4T3=`g6somWrB!^@SFPBpr4OsMYO7Uh&0TAsGn1J~@Or=7-uvC} z%Qt83z1Ci9{cEqa&OSSje!TyF0CVtx2}QuSF4!DwvCG#4+Y%Sp_L;$U+k_@ius+xo zEDr}Gt>w#_*4xbqf#DtD;3hk^FxnZRk*E_eote0Rzh26BCPLxz`LS4VQ++6&s1_I& z2(?CniO!fUFnuVQn);S#doUC!UoMqb&!t6q@nDzT!mhMOTWF!rSty+eBx0dRD{F2g zexS{^$rqz06bU8f3d|_28!X#6cxuLK0c{~E_dItB zRV#0rpQbbLySRo?#7?(R1#(Sm3q}ZjUMfY-qG7d{Qg@yS+U((OsCn8#1#h9@B&5Jd zGBMxrzzGofF(z$rP@2v<2CF8+!MaGoZna~Sq3pt93-eJcFuK$QF-WJ6T&RuNHb=qr zffv?UIFlC^5xYLxyuog%i{yyZTUf#(!^6aPxa7W3TGuE8IS0!vEXBVG=%qYE&lV@r zN+T7e#ft59i50X1ZuINe@HrM%;#}1`)~{u^&$o~OM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Bj~H_HT(vKN26~;=9(TaeWx8E$|G+y4r56_wFc9<(YQ!nYS6t#x1f(F z#hO8tFa}006T-wx;v@RU-^9uOzhmoBm^_%ogH7&0a@J>Z8Z$Zd1DrXMEE1Ea6qUK; zlqp5y24=l3Sv;jkISNYT0uzVGye^JEioAZhRyu}U&rUZ~)A_&q&;NLM$1qR+Fp V{vZCnjduwFU#tBE@8NGK{!eI%?A`zX delta 894 zcmaKq$x6de6o&t#RcMmN+QBMG>wq|v(v6~_Qm__kr4HZ>?nFdH!5I{_=+c#NaUs5f z3kxL+7gDzhzKU-ko}@9>7$uOKoO{oI{_mW7lX;H_FCULj05Ui)3#@@%`{FDp4sasN ziyJW+UI=JH0AVzv4I#86f+1SR(8^xL)xab?;3+?xs359%*Vu@p=2bcnM=yn2YMbao zKmE}$jG_@7Jz0|A;iDLj92EcxPN_Rdjdr=qus2D`ITLgYRuQW}(@}UZg?s32lmi)X zwZjsArSy+Z(W#Si*RilJFokKVXW%Rmj)&HW)fwAeQpZf0Hh38J1hA^#TNtF)44K*; zwJf`35lfqtFpD`#<1mi}tBTV*6J#}-b~ni~vJ%*%ks1R_e+_J1?&fK3SF);%lA~;0 zZrV1HX3uh{bZH@J4x|-A+H}TrZ2t)9*!iXok_%v*pbJ!Q0>dcQ!E1J1&4$;o-wDa2 zKfvK%^jl48X{L@KY%6`=ykon0yToJkBKgB;1uHs^ZV~+*J-PSX(|M0MIoTZ40^RXN bB(VHd)wpL#yKK?|NSSF|f{dTiCy%pFD1C6@ diff --git a/src/csv/highscores.csv b/src/csv/highscores.csv new file mode 100644 index 0000000..e69de29 diff --git a/src/facade/YahtzeeGame.java b/src/facade/YahtzeeGame.java index 95ca70a..ee40a43 100644 --- a/src/facade/YahtzeeGame.java +++ b/src/facade/YahtzeeGame.java @@ -1,6 +1,11 @@ package facade; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.time.LocalDate; import java.util.ArrayList; +import java.util.Scanner; import domain.Player; @@ -25,66 +30,310 @@ public class YahtzeeGame { public void rollDices(int player) { - players.get(player - 1).hand.rollDices(); + players.get(player).hand.rollDices(); } public ArrayList getAllDiceNumbers(int player) { - return players.get(player - 1).hand.getAllDiceNumbers(); + return players.get(player).hand.getAllDiceNumbers(); } public ArrayList getFreeDiceNumbers(int player) { - return players.get(player - 1).hand.getFreeDiceNumbers(); + return players.get(player).hand.getFreeDiceNumbers(); } public ArrayList getLockedInDiceNumbers(int player) { - return players.get(player - 1).hand.getLockedInDiceNumbers(); + return players.get(player).hand.getLockedInDiceNumbers(); } public void lockInDices(int player, int... dices) { - players.get(player - 1).hand.lockInDices(dices); + players.get(player).hand.lockInDices(dices); } public int getLeftRolls(int player) { - return players.get(player - 1).hand.getLeftRolls(); + return players.get(player).hand.getLeftRolls(); } public ArrayList getAllOptionsEvaluated(int player) { - players.get(player - 1).box.evaluateBoxOptions(players.get(player - 1).hand.getAllDiceNumbers()); - return players.get(player - 1).box.getAllOptions(); + players.get(player).box.evaluateBoxOptions(players.get(player - 1).hand.getAllDiceNumbers()); + return players.get(player).box.getAllOptions(); } public void selectOption(int player, String selectedOption) { - players.get(player - 1).box.selectOption(selectedOption); - players.get(player - 1).box.resetOptions(); - players.get(player - 1).getNewHand(); + players.get(player).box.selectOption(selectedOption); + players.get(player).box.resetOptions(); + players.get(player).getNewHand(); } public boolean gameOver(int player) { - return players.get(player - 1).box.gameOver(); + return players.get(player).box.gameOver(); } public int returnTotalPoints(int player) { - if (players.get(player - 1).box.gameOver()) - return players.get(player - 1).box.returnTotalPoints(); + if (players.get(player).box.gameOver()) + return players.get(player).box.returnTotalPoints(); else return 0; } + + public void setScoreToHighscores(int player) throws FileNotFoundException { + + String highscores[][] = new String[10][3]; + String highscoreMem1 = ""; + String highscoreMem2 = ""; + Scanner sc = new Scanner("src/csv/highscores.csv"); + + for (int i = 0; i < 10; i++) { + + if (sc.hasNextLine()) { + highscores[i][0] = sc.next(); + highscores[i][1] = sc.next(); + highscores[i][2] = sc.nextLine(); + } + } + + for (int i = 0; i < 10; i++) { + + if (highscores[i][2] == null) { + highscores[i][0] = sc.next(); + highscores[i][1] = sc.next(); + highscores[i][2] = sc.nextLine(); + } + } + + try { + if (returnTotalPoints(player) < Integer.parseInt(highscores[9][2])) { + + return; + + } + } catch (Exception e) { + } + + try { + + if (returnTotalPoints(player) > Integer.parseInt(highscores[0][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem1 = highscores[0][i]; + if (i == 2) + highscores[0][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[0][i] = "" + players.get(player).name; + else if (i == 0) + highscores[0][i] = "" + LocalDate.now(); + highscoreMem2 = highscores[1][i]; + highscores[1][i] = highscoreMem1; + highscoreMem1 = highscores[2][i]; + highscores[2][i] = highscoreMem2; + highscoreMem2 = highscores[3][i]; + highscores[3][i] = highscoreMem1; + highscoreMem1 = highscores[4][i]; + highscores[4][i] = highscoreMem2; + highscoreMem2 = highscores[5][i]; + highscores[5][i] = highscoreMem1; + highscoreMem1 = highscores[6][i]; + highscores[6][i] = highscoreMem2; + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[1][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem2 = highscores[1][i]; + if (i == 2) + highscores[1][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[1][i] = "" + players.get(player).name; + else if (i == 0) + highscores[1][i] = "" + LocalDate.now(); + highscoreMem1 = highscores[2][i]; + highscores[2][i] = highscoreMem2; + highscoreMem2 = highscores[3][i]; + highscores[3][i] = highscoreMem1; + highscoreMem1 = highscores[4][i]; + highscores[4][i] = highscoreMem2; + highscoreMem2 = highscores[5][i]; + highscores[5][i] = highscoreMem1; + highscoreMem1 = highscores[6][i]; + highscores[6][i] = highscoreMem2; + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[2][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem1 = highscores[2][i]; + if (i == 2) + highscores[2][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[2][i] = "" + players.get(player).name; + else if (i == 0) + highscores[2][i] = "" + LocalDate.now(); + highscoreMem2 = highscores[3][i]; + highscores[3][i] = highscoreMem1; + highscoreMem1 = highscores[4][i]; + highscores[4][i] = highscoreMem2; + highscoreMem2 = highscores[5][i]; + highscores[5][i] = highscoreMem1; + highscoreMem1 = highscores[6][i]; + highscores[6][i] = highscoreMem2; + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[3][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem2 = highscores[3][i]; + if (i == 2) + highscores[3][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[3][i] = "" + players.get(player).name; + else if (i == 0) + highscores[3][i] = "" + LocalDate.now(); + highscoreMem1 = highscores[4][i]; + highscores[4][i] = highscoreMem2; + highscoreMem2 = highscores[5][i]; + highscores[5][i] = highscoreMem1; + highscoreMem1 = highscores[6][i]; + highscores[6][i] = highscoreMem2; + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[4][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem1 = highscores[4][i]; + if (i == 2) + highscores[4][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[4][i] = "" + players.get(player).name; + else if (i == 0) + highscores[4][i] = "" + LocalDate.now(); + highscoreMem2 = highscores[5][i]; + highscores[5][i] = highscoreMem1; + highscoreMem1 = highscores[6][i]; + highscores[6][i] = highscoreMem2; + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[5][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem2 = highscores[5][i]; + if (i == 2) + highscores[5][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[5][i] = "" + players.get(player).name; + else if (i == 0) + highscores[5][i] = "" + LocalDate.now(); + highscoreMem1 = highscores[6][i]; + highscores[6][i] = highscoreMem2; + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[6][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem1 = highscores[6][i]; + if (i == 2) + highscores[6][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[6][i] = "" + players.get(player).name; + else if (i == 0) + highscores[6][i] = "" + LocalDate.now(); + highscoreMem2 = highscores[7][i]; + highscores[7][i] = highscoreMem1; + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[7][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem2 = highscores[7][i]; + if (i == 2) + highscores[7][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[7][i] = "" + players.get(player).name; + else if (i == 0) + highscores[7][i] = "" + LocalDate.now(); + highscoreMem1 = highscores[8][i]; + highscores[8][i] = highscoreMem2; + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[8][2])) { + + for (int i = 2; i >= 0; i--) { + highscoreMem1 = highscores[8][i]; + if (i == 2) + highscores[8][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[8][i] = "" + players.get(player).name; + else if (i == 0) + highscores[8][i] = "" + LocalDate.now(); + highscores[9][i] = highscoreMem1; + } + } else if (returnTotalPoints(player) > Integer.parseInt(highscores[9][2])) { + + for (int i = 2; i >= 0; i--) { + if (i == 2) + highscores[9][i] = "" + returnTotalPoints(player); + else if (i == 1) + highscores[9][i] = "" + players.get(player).name; + else if (i == 0) + highscores[9][i] = "" + LocalDate.now(); + } + } + + PrintWriter out = new PrintWriter("src/csv/highscores.csv"); + + for (int i = 0; i < 10; i++) { + for (int s = 0; s < 3; s++) { + out.print(i + ": " + LocalDate.now() + ": " + players.get(player).name + ": " + + returnTotalPoints(player)); + } + out.println(); + } + + out.close(); + } catch (Exception e) { + } + } }