Skip to content

Instantly share code, notes, and snippets.

@wertalp
Created September 14, 2015 22:32
Show Gist options
  • Save wertalp/f7197eb73525974de54f to your computer and use it in GitHub Desktop.
Save wertalp/f7197eb73525974de54f to your computer and use it in GitHub Desktop.
Version before extends Layout
package com.example.wertalp.battleship;
/**
* Created by wertalp on 31.08.15.
*/
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
/**
* Created by wertalp on 26.08.15.
*/
public class ChessBoard {
Activity activity ;
static GridLayout board ;
Display display ;
LayoutInflater inflater ;
PopupWindow pw ;
Button Butty ;
CellButton[] liCellBut ;
int heigth ;
int width ;
int laenge ;
int breite ;
static final String[] rowNames = { "A","B","C","D","E","F","G","H","I","J","K",
"L","M","N","O","P","R","S","T","U","V","W"};
public ChessBoard(Activity activity) {
this.activity = activity;
this.display = activity.getWindowManager().getDefaultDisplay();
this.heigth = display.getHeight();
this.width = display.getWidth() ;
this.create();
}
public int getHeigth() {
return heigth;
}
public void setHeigth(int heigth) {
this.heigth = heigth;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void create() {
int width = display.getWidth();
int height =display.getHeight();
board = (GridLayout) activity.findViewById(com.example.wertalp.battleship.R.id.board);
board.removeAllViews();
Button buttonSet = (Button) activity.findViewById(com.example.wertalp.battleship.R.id.button23);
EditText edit1 = (EditText) activity.findViewById(com.example.wertalp.battleship.R.id.editText);
//board.setColumnCount(Integer.parseInt(edit1.getText().toString()));
//board.setRowCount(Integer.parseInt(edit1.getText().toString()));
board.setColumnCount(11);
board.setRowCount(11);
laenge = (int) (this.getWidth()-300)/board.getColumnCount();
breite = laenge ;
for (int i = 0; i <= board.getRowCount()-1 ; i++) {
for (int j = 0; j <= board.getColumnCount()-1 ; j++) {
CellButton button = new CellButton(activity);
button.setBackgroundResource(com.example.wertalp.battleship.R.drawable.rectangle);
button.setId(View.generateViewId());
button.setyField(j) ;
button.setxField(i) ;
//button.setText(""+i+j);
button.setLayoutParams(new LinearLayout.LayoutParams(laenge, breite));
board.addView(button);
//anz++;
if (i == 0) {
button.setText("" + j);
button.setBackgroundResource(com.example.wertalp.battleship.R.drawable.rectangle_second);
}
if (j == 0) {
button.setText("" + rowNames[i]);
button.setBackgroundResource(com.example.wertalp.battleship.R.drawable.rectangle_second);
}
if ((i != 0) &&(j !=0)) {
;};
}
} // create Methode ende
} // End Create
public CellButton[] getallcells(){
int l = 0;
liCellBut = new CellButton[board.getChildCount()];
for (int i =0;i<= board.getChildCount();i++){
View tmpView = (View) board.getChildAt(i);
//Toast.makeText(getActivity(),tmpView.getClass().getName(),Toast.LENGTH_SHORT).show();
if (tmpView instanceof CellButton){
//Toast.makeText(getActivity(),"CellButt",Toast.LENGTH_SHORT).show();
liCellBut[l] = (CellButton) tmpView;
l++;
}
}
return liCellBut;
}
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
// 13 14 15 16
//2/3
//y-1*x+x
public static CellButton[] getCellonPosition(int x,int y) {
Integer cols = board.getColumnCount() ;
Integer rows = board.getRowCount() ;
int position = y*x+x ;
CellButton[] shipitems = new CellButton[3];
//View myview =(View) board.getChildAt(1 + (int) (Math.random() * 225));
for (int i = 0; i <3 ; i++) {
View myview =(View) board.getChildAt(position+i);
shipitems[i]= (CellButton) myview;
}
// Toast.makeText(getActivity().getApplicationContext(), "cols: " + cols + " rows: " + rows, Toast.LENGTH_SHORT).show();
return shipitems;
}
public Activity getActivity () {
return activity;
}
public void setActivity (Activity activity){
this.activity = activity;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment