Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. package SpaceBattle;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.util.Random;
  9.  
  10. //import javax.smartcardio.Card;
  11. import javax.swing.*;
  12.  
  13. public class spacebattle2 extends JFrame {
  14.  
  15. JLabel[] tilelabel = new JLabel[31];
  16. JLabel[] hexlabel = new JLabel[31];
  17.  
  18.  
  19.  
  20. ImageIcon tileicon, hexicon;
  21.  
  22. int rownum, colnum;
  23. int c, r, xloc, yloc, hexctr, tilectr;
  24. int energy, ore, capacity;
  25. String labelname;
  26. String type;
  27.  
  28. int numtiles = 31;
  29. int deckctr = 26;
  30.  
  31.  
  32. String[] tiledeck = new String[numtiles];
  33. int[] tilexloc = new int[numtiles];
  34. int[] tileyloc = new int[numtiles];
  35.  
  36. int tileenergy[] = new int[numtiles];
  37. int tileore[] = new int[numtiles];
  38. int tilecapacity[] = new int[numtiles];
  39. String tiletype[] = new String[numtiles];
  40. String tilename[] = new String[numtiles];
  41. int tilemeeple[] = new int[numtiles];
  42.  
  43.  
  44.  
  45. String[] hexnum = new String[numtiles];
  46. int[] hexxloc = new int[numtiles];
  47. int[] hexyloc = new int[numtiles];
  48. String[] hexempty = new String[numtiles];
  49.  
  50. int hexenergy[] = new int[numtiles];
  51. int hexore[] = new int[numtiles];
  52. int hexcapacity[] = new int[numtiles];
  53. String hextype[] = new String[numtiles];
  54. String hexname[] = new String[numtiles];
  55. String hexplayer[] = new String[numtiles];
  56.  
  57. String[] players = new String[6];
  58. String playerturn;
  59.  
  60. int startingplanets = 2;
  61. int bluemaxplanet = startingplanets;
  62. int redmaxplanet = startingplanets;
  63.  
  64. int numplanets = 7;
  65. int numspace = 24;
  66. int nonplayertiles = numplanets + numspace;
  67.  
  68. String planetCode;
  69. String planetselected = "none";
  70.  
  71. //selectPlanet
  72. String player = "Red";
  73.  
  74. int i;
  75.  
  76.  
  77.  
  78.  
  79. public spacebattle2() {
  80.  
  81. setLayout(null);
  82.  
  83. createDeck();
  84. shuffleDeck();
  85. drawBoard();
  86. //selectPlanet();
  87.  
  88. }
  89.  
  90.  
  91. private void selectPlanet() {
  92. // TODO Auto-generated method stub
  93.  
  94. for(i = 0; i < numtiles; i++) {
  95.  
  96. tilelabel[i].addMouseListener(new MouseListener() {
  97.  
  98. public void mouseClicked(MouseEvent e) {
  99.  
  100.  
  101. System.out.println(i);
  102.  
  103.  
  104. }
  105.  
  106.  
  107. public void mouseEntered(MouseEvent arg0) {
  108. // TODO Auto-generated method stub
  109.  
  110.  
  111. }
  112.  
  113. public void mouseExited(MouseEvent arg0) {
  114. // TODO Auto-generated method stub
  115.  
  116. }
  117.  
  118. public void mousePressed(MouseEvent arg0) {
  119. // TODO Auto-generated method stub
  120.  
  121. }
  122.  
  123. public void mouseReleased(MouseEvent arg0) {
  124. // TODO Auto-generated method stub
  125.  
  126. }
  127. });
  128. }
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. private void shuffleDeck() {
  140. // TODO Auto-generated method stub
  141.  
  142. System.out.println("shuffling deck...");
  143.  
  144. Random r = new Random();
  145. int swapIndex;
  146.  
  147. String temp;
  148.  
  149. for(int startIndex = 0; startIndex < numtiles; startIndex++) {
  150.  
  151. System.out.println(startIndex + " startIndex " + tiledeck[startIndex]);
  152.  
  153. swapIndex = r.nextInt(numtiles);
  154. System.out.println(swapIndex + " swapIndex " + tiledeck[swapIndex]);
  155.  
  156. if(swapIndex != startIndex) {
  157.  
  158. temp = tiledeck[swapIndex];
  159.  
  160. tiledeck[swapIndex] = tiledeck[startIndex];
  161. System.out.println("Moving " + startIndex + " " + tiledeck[startIndex] + " startIndex to " + swapIndex + " swapIndex");
  162.  
  163. tiledeck[startIndex] = temp;
  164. System.out.println("Moving " + temp + " temp to " + startIndex + " startIndex");
  165. }
  166. }
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. private void createDeck() {
  174. // TODO Auto-generated method stub
  175.  
  176. int s, p, xloc = 667, yloc = 10;
  177.  
  178.  
  179.  
  180.  
  181. /*******************************
  182. *
  183. * Create Space tiles
  184. *
  185. ******************************/
  186.  
  187. System.out.println("creating space tiles...");
  188.  
  189. tilectr = 0;
  190.  
  191. for(s= 0; s < numspace; s++) {
  192.  
  193. tiledeck[tilectr] = "000";
  194.  
  195. System.out.println(tilectr + " " + tiledeck[tilectr]);
  196.  
  197. tilectr++;
  198. }
  199.  
  200.  
  201. /**************************
  202. *
  203. * Create Planet tiles
  204. *
  205. *************************/
  206.  
  207. System.out.println("creating planet tiles...");
  208.  
  209. for(p = 0; p < numplanets; p++) {
  210.  
  211. energy = (int) (Math.random()*3 + 1);
  212. ore = (int) (Math.random()*3 +1);
  213. capacity = (int) (Math.random()*3 +1);
  214.  
  215.  
  216. StringBuffer stringBuffer = new StringBuffer();
  217. stringBuffer.append("");
  218. stringBuffer.append(energy);
  219. stringBuffer.append(ore);
  220. stringBuffer.append(capacity);
  221.  
  222. String planetCode = stringBuffer.toString();
  223.  
  224. tiledeck[tilectr] = planetCode;
  225.  
  226. System.out.println(tilectr + " " + tiledeck[tilectr]);
  227. tilectr++;
  228. }
  229.  
  230. }
  231.  
  232.  
  233.  
  234. private void drawBoard() {
  235. // TODO Auto-generated method stub
  236. int xloc = 10;
  237. int yloc = 61;
  238. int hexctr = 0;
  239.  
  240.  
  241. int colnum = 7;
  242. int rownum = 4;
  243.  
  244. xloc = 10;
  245. yloc = 61;
  246. hexctr = 0;
  247.  
  248.  
  249. colnum = 7;
  250. rownum = 4;
  251.  
  252. int row = 0, col = 0, numtiles = 31;
  253.  
  254. for(int c = 0; c < numtiles; c ++) {
  255. if(tiledeck[c] == "000") {
  256. System.out.println(c + " " + tiledeck[c] + " tile is space");
  257. tileenergy[c] = 0;
  258. tileore[c] = 0;
  259. tilecapacity[c] = 0;
  260. tiletype[c] = "space";
  261. tilename[c] = "space";
  262.  
  263. tileicon = new ImageIcon(getClass().getResource("/000.png"));
  264. tilelabel[c] = new JLabel();
  265. tilelabel[c].setIcon(tileicon);
  266.  
  267. tilelabel[c].setBounds(xloc, yloc, 115, 100);
  268.  
  269. add(tilelabel[c]);
  270.  
  271. yloc = yloc + 102;
  272. row++;
  273.  
  274. if(row == rownum) {
  275. xloc = xloc + 88;
  276. row = 0;
  277. col++;
  278. if(col % 2 == 0) {
  279. yloc = 61;
  280. rownum = rownum - 1;
  281. }
  282. else {
  283. yloc = 10;
  284. rownum = rownum + 1;
  285. }
  286. }
  287. }
  288.  
  289.  
  290.  
  291. else {
  292. System.out.println(c + " " + tiledeck[c] + " tile is planet");
  293. tileenergy[c] = Integer.parseInt(tiledeck[c].substring(0, 1));
  294. tileore[c] = Integer.parseInt(tiledeck[c].substring(1, 2));
  295. tilecapacity[c] = Integer.parseInt(tiledeck[c].substring(2, 3));
  296. tiletype[c] = "planet";
  297. tilemeeple[c] = 0;
  298.  
  299. tileicon = new ImageIcon(getClass().getResource("/" + tiledeck[c] + ".png"));
  300. tilelabel[c] = new JLabel();
  301. tilelabel[c].setIcon(tileicon);
  302. tilelabel[c].setBounds(xloc, yloc, 115, 100);
  303. add(tilelabel[c]);
  304.  
  305. yloc = yloc + 102;
  306. row++;
  307.  
  308. if(row == rownum) {
  309. xloc = xloc + 88;
  310. row = 0;
  311. col++;
  312. if(col % 2 == 0) {
  313. yloc = 61;
  314. rownum = rownum - 1;
  315. }
  316. else {
  317. yloc = 10;
  318. rownum = rownum + 1;
  319. }
  320. }
  321. }
  322. }
  323. }
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347. private Object i(int numtiles2, int i) {
  348. // TODO Auto-generated method stub
  349. return null;
  350. }
  351.  
  352.  
  353. public static void main(String[] args) {
  354. // TODO Auto-generated method stub
  355. spacebattle2 board = new spacebattle2();
  356. board.setSize(900, 600);
  357. board.setResizable(false);
  358. board.setTitle("Space Battle");
  359. board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  360. board.setVisible(true);
  361. }//End void main
  362.  
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement