-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
130 lines (116 loc) · 4.17 KB
/
Copy pathBoard.java
File metadata and controls
130 lines (116 loc) · 4.17 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.example.itay.sumika;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by itay on 8/11/17.
*/
public class Board {
final int numOfColumnsX, numOfRowsY;
final int spaceHorizontal = 4; //space between buttons- for width calculations.
int buttonSize;//px;
int buttonFontSize;//px
GridView boardGrid;
GridViewAdapter gridViewAdapter;
GameScreen gameScreen;
boolean onePlayer;
int compLevel, validSpace, timeForMove;
Context context;
private Cell[][] cells;
public Board(int numOfColumnsX,
int numOfRowsY,
GridView gridView,
Context context,
int compLevel,
boolean onePlayer,
GameScreen gameScreen,
int validSpace,
int timeForMove) {
this.numOfColumnsX = numOfColumnsX;
this.numOfRowsY = numOfRowsY;
this.context = context;
this.compLevel=compLevel;
this.validSpace=validSpace;
this.timeForMove=timeForMove;
this.onePlayer=onePlayer;
this.boardGrid = gridView;
// this.cells = new Cell[numOfColumnsX][numOfRowsY];
boardGrid.setNumColumns(numOfColumnsX);
this.gameScreen=gameScreen;
}
public void createBoard() {
editGeneralButtonSizeInPx();
boardGrid = initializeGridViewWidth(boardGrid, spaceHorizontal, numOfRowsY, buttonSize);
this.gridViewAdapter = new GridViewAdapter(numOfColumnsX,
numOfRowsY,
context,
buttonSize,
buttonFontSize,
gameScreen,
onePlayer,
compLevel,
validSpace,
timeForMove);
boardGrid.setAdapter(gridViewAdapter);
cells = gridViewAdapter.getCells();
}
public int[][] cellsToInt(){
int[][] cellsInInt=new int[numOfColumnsX][numOfRowsY];
for (int y = 0; y < numOfRowsY; y++) {
for (int x = 0; x < numOfColumnsX; x++) {
cellsInInt[x][y] = 0;
}
}
return cellsInInt;
}
public static int convertDpToPixels(float dp, Context context) {
Resources resources = context.getResources();
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
resources.getDisplayMetrics()
);
}
public void editGeneralButtonSizeInPx() {
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
buttonSize = screenWidth / (numOfRowsY+1);
buttonFontSize= buttonSize /2 ;
}
public void drawTheMove(Point p, int numOfClick, Player lastPlayerPlayed) {
Button btn=cells[p.x][p.y].getButton();
btn.setText(Integer.toString(numOfClick));
btn.setBackgroundResource(lastPlayerPlayed.getImageId());
cells[p.x][p.y].setButton(btn);
gameScreen.board.gridViewAdapter.refresh(cells,p);
gameScreen.board.boardGrid.invalidateViews();
}
private GridView initializeGridViewWidth(GridView boardGrid, int spaceHorizontal, int numOfColumns, int buttonSize) {
int neWidth = numOfColumns * (spaceHorizontal);
int neWidthPx = convertDpToPixels(neWidth, context) + buttonSize * numOfColumns;
ViewGroup.LayoutParams layoutParams = boardGrid.getLayoutParams();
layoutParams.width = neWidthPx; //this is in pixels
boardGrid.setLayoutParams(layoutParams);
boardGrid.setColumnWidth(buttonSize);
return boardGrid;
}
//a function to operate
public void addCell(int column,int row,Cell cell){
cells[column][row]=cell;
}
public void addButtonToCell(Button btn,int posx,int posy){
this.cells[posx][posy].setButton(btn);
}
public void setCells(Cell[][] cells){
this.cells=cells;
}
public Cell[][] getCells() {
return cells;
}
}