Хорошо, я не могу понять эту ошибку. Это очень специфично, и я не могу найти что-нибудь, чтобы помочь мне онлайн. Я делаю игру в шашки в Android Studio с GridView
и ImageAdapter
. Мой код, вероятно, беспорядок для большинства людей, но, надеюсь, кто-то может прочитать его и выяснить мою проблему.
Проблема в том, что мои шашки не будут перемещаться в определенные места на доске. Зеленые точки на этом изображении - это точки, на которые он отказывается двигаться: https://i.imgur.com/y81XdKy.jpg.
Все в OnItemClickListener
- логика игры в шашки, и она перерисовывает игровое поле внутри, сбрасывая ImageAdapter
.
РЕДАКТИРОВАТЬ: Меня попросили сократить код до того места, где, по моему мнению, лежит проблема, но я не совсем уверен, где она лежит. Проблема в том, что один из фрагментов, выделенных с выделенным окружением, не может быть перемещен в выделенный фрагмент, как показано выше на изображении. Я искренне верю, что это проблема, связанная с тем, что сетка не определяет, какой элемент был выбран, хотя я могу нажать любую другую плитку, чтобы отменить выбор выбранного элемента. Я собираюсь отрезать бит движения, потому что именно там оно, скорее всего, и лежит, но я также оставлю весь остальной код без изменений под ним.
Здесь перемещается фрагмент кода, при котором он не будет работать при попытке перемещения фрагмента в выбранных зеленых точках на изображении :
if (placement[position] == 2) //MOVEMENT OF PIECE
{
if ((placement[position + 14] == 6 || placement[position + 14] == 10) && (placement[position + 7] == 3 || placement[position + 7] == 7) && placement[position] == 2) {
placement[position + 7] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
} else if ((placement[position + 18] == 6 || placement[position + 18] == 10) && (placement[position + 9] == 3 || placement[position + 9] == 7) && placement[position] == 2) {
placement[position + 9] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
} else if (placement[position - 14] == 10 && (placement[position - 7] == 3 || placement[position + 7] == 7) && placement[position] == 2) {
placement[position - 7] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
} else if (placement[position - 18] == 10 && (placement[position - 9] == 3 || placement[position - 9] == 7) && placement[position] == 2) {
placement[position - 9] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
}
for (int i = 0; i < placement.length; i++) {
if (placement[i] == 6) {
placement[position] = 4;
placement[i] = 0;
redTurn = false;
turnTeller.setText("White's Turn");
} else if (placement[i] == 10) {
placement[position] = 8;
placement[i] = 0;
redTurn = false;
turnTeller.setText("White's Turn");
}
if (placement[i] == 2) {
placement[i] = 0;
}
}
}
Вот код (Заранее извините, скорее всего, плохо):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int[] placement = {
1, 3, 1, 3, 1, 3, 1, 3,
3, 1, 3, 1, 3, 1, 3, 1,
1, 3, 1, 3, 1, 3, 1, 3,
0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0,
4, 1, 4, 1, 4, 1, 4, 1,
1, 4, 1, 4, 1, 4, 1, 4,
4, 1, 4, 1, 4, 1, 4, 1 };
ImageView pic;
ImageAdapter adapter = new ImageAdapter(this);
int redScore = 0;
int whiteScore = 0;
boolean redTurn = true;
boolean ai = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView board = (GridView) findViewById(R.id.board);
board.setAdapter(adapter);
final TextView turnTeller = (TextView)findViewById(R.id.Turn);
if(!ai)
turnTeller.setText("Red's Turn");
else
turnTeller.setText("");
board.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (redTurn) {
if (placement[position] != 2) //DESELECT PIECE
{
for (int i = 0; i < placement.length; i++) {
if (placement[i] == 2) {
placement[i] = 0;
} else if (placement[i] == 6) {
placement[i] = 4;
} else if (placement[i] == 10) {
placement[i] = 8;
}
}
}
if (placement[position] == 4 || placement[position] == 8) //SELECT PIECE
{
if (placement[position] == 4) {
placement[position] = 6;
} else {
placement[position] = 10;
}
if (position != 8 && position != 24 && position != 40 && position != 56 && position != 63 && position != 47 && position != 31 && position != 15) {
if (placement[position - 7] == 0) {
placement[position - 7] = 2;
}
if (placement[position - 9] == 0) {
placement[position - 9] = 2;
}
if (placement[position - 7] == 3) {
if (placement[position - 14] == 0) {
placement[position - 14] = 2;
}
}
if (placement[position - 9] == 3) {
if (placement[position - 18] == 0) {
placement[position - 18] = 2;
}
}
if (placement[position] == 10) {
if (placement[position + 7] == 0) {
placement[position + 7] = 2;
}
if (placement[position + 9] == 0) {
placement[position + 9] = 2;
}
if (placement[position + 7] == 3) {
if (placement[position + 14] == 0) {
placement[position + 14] = 2;
}
}
if (placement[position + 9] == 3) {
if (placement[position + 18] == 0) {
placement[position + 18] = 2;
}
}
}
} else if (position == 8 || position == 24 || position == 40 || position == 56 || position == 63) {
if (placement[position - 7] == 0) {
placement[position - 7] = 2;
}
if (placement[position - 7] == 3) {
if (placement[position - 14] == 0) {
placement[position - 14] = 2;
}
}
if (placement[position] == 10) {
if (placement[position + 7] == 0) {
placement[position + 7] = 2;
}
if (placement[position + 7] == 3) {
if (placement[position + 14] == 0) {
placement[position + 14] = 2;
}
}
}
} else {
if (placement[position - 9] == 0) {
placement[position - 9] = 2;
}
if (placement[position - 9] == 3) {
if (placement[position - 18] == 0) {
placement[position - 18] = 2;
}
}
if (placement[position] == 10) {
if (placement[position + 9] == 0) {
placement[position + 9] = 2;
}
if (placement[position + 9] == 3) {
if (placement[position + 18] == 0) {
placement[position + 18] = 2;
}
}
}
}
}
if (placement[position] == 2) //MOVEMENT OF PIECE
{
if ((placement[position + 14] == 6 || placement[position + 14] == 10) && (placement[position + 7] == 3 || placement[position + 7] == 7) && placement[position] == 2) {
placement[position + 7] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
} else if ((placement[position + 18] == 6 || placement[position + 18] == 10) && (placement[position + 9] == 3 || placement[position + 9] == 7) && placement[position] == 2) {
placement[position + 9] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
} else if (placement[position - 14] == 10 && (placement[position - 7] == 3 || placement[position + 7] == 7) && placement[position] == 2) {
placement[position - 7] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
} else if (placement[position - 18] == 10 && (placement[position - 9] == 3 || placement[position - 9] == 7) && placement[position] == 2) {
placement[position - 9] = 0;
redScore++;
redTurn = false;
turnTeller.setText("White's Turn");
}
for (int i = 0; i < placement.length; i++) {
if (placement[i] == 6) {
placement[position] = 4;
placement[i] = 0;
redTurn = false;
turnTeller.setText("White's Turn");
} else if (placement[i] == 10) {
placement[position] = 8;
placement[i] = 0;
redTurn = false;
turnTeller.setText("White's Turn");
}
if (placement[i] == 2) {
placement[i] = 0;
}
}
}
if (placement[1] == 4) //KING RED
{
placement[1] = 8;
} else if (placement[3] == 4) {
placement[3] = 8;
} else if (placement[5] == 4) {
placement[5] = 8;
} else if (placement[7] == 4) {
placement[7] = 8;
}
board.setAdapter(adapter);
}
else if(ai == false)
{
if (placement[position] != 2) //DESELECT PIECE
{
for (int i = 0; i < placement.length; i++) {
if (placement[i] == 2) {
placement[i] = 0;
} else if (placement[i] == 5) {
placement[i] = 3;
} else if (placement[i] == 9) {
placement[i] = 7;
}
}
}
if (placement[position] == 3 || placement[position] == 7) //SELECT PIECE
{
if (placement[position] == 3) {
placement[position] = 5;
} else {
placement[position] = 9;
}
if (position != 8 && position != 24 && position != 40 && position != 56 && position != 63 && position != 47 && position != 31 && position != 15) {
if (placement[position + 7] == 0) {
placement[position + 7] = 2;
}
if (placement[position + 9] == 0) {
placement[position + 9] = 2;
}
if (placement[position + 7] == 4) {
if (placement[position + 14] == 0) {
placement[position + 14] = 2;
}
}
if (placement[position + 9] == 4) {
if (placement[position + 18] == 0) {
placement[position + 18] = 2;
}
}
if (placement[position] == 9) {
if (placement[position - 7] == 0) {
placement[position - 7] = 2;
}
if (placement[position - 9] == 0) {
placement[position - 9] = 2;
}
if (placement[position - 7] == 4) {
if (placement[position - 14] == 0) {
placement[position - 14] = 2;
}
}
if (placement[position - 9] == 4) {
if (placement[position - 18] == 0) {
placement[position - 18] = 2;
}
}
}
} else if (position == 8 || position == 24 || position == 40 || position == 56 || position == 63) {
if (placement[position + 7] == 0) {
placement[position + 7] = 2;
}
if (placement[position + 7] == 4) {
if (placement[position + 14] == 0) {
placement[position + 14] = 2;
}
}
if (placement[position] == 9) {
if (placement[position - 7] == 0) {
placement[position - 7] = 2;
}
if (placement[position - 7] == 4) {
if (placement[position - 14] == 0) {
placement[position - 14] = 2;
}
}
}
} else {
if (placement[position + 9] == 0) {
placement[position + 9] = 2;
}
if (placement[position + 9] == 4) {
if (placement[position + 18] == 0) {
placement[position + 18] = 2;
}
}
if (placement[position] == 9) {
if (placement[position - 9] == 0) {
placement[position - 9] = 2;
}
if (placement[position - 9] == 4) {
if (placement[position - 18] == 0) {
placement[position - 18] = 2;
}
}
}
}
}
if (placement[position] == 2) //MOVEMENT OF PIECE
{
if ((placement[position - 14] == 5 || placement[position - 14] == 9) && (placement[position - 7] == 4 || placement[position - 7] == 8)&& placement[position] == 2) {
placement[position - 7] = 0;
whiteScore++;
redTurn = true;
turnTeller.setText("Red's Turn");
} else if ((placement[position - 18] == 5 || placement[position - 18] == 9) && (placement[position - 9] == 4 || placement[position - 7] == 8)&& placement[position] == 2) {
placement[position - 9] = 0;
whiteScore++;
redTurn = true;
turnTeller.setText("Red's Turn");
} else if (placement[position + 14] == 9 && (placement[position + 7] == 4 || placement[position + 7] == 8)&& placement[position] == 2) {
placement[position + 7] = 0;
whiteScore++;
redTurn = true;
turnTeller.setText("Red's Turn");
} else if (placement[position + 18] == 9 && (placement[position + 9] == 4 || placement[position + 9] == 8) && placement[position] == 2) {
placement[position + 9] = 0;
whiteScore++;
redTurn = true;
turnTeller.setText("Red's Turn");
}
for (int i = 0; i < placement.length; i++) {
if (placement[i] == 5) {
placement[position] = 3;
placement[i] = 0;
redTurn = true;
turnTeller.setText("Red's Turn");
} else if (placement[i] == 9) {
placement[position] = 7;
placement[i] = 0;
redTurn = true;
turnTeller.setText("Red's Turn");
}
if (placement[i] == 2) {
placement[i] = 0;
}
}
}
if (placement[1] == 56) //KING RED
{
placement[1] = 7;
} else if (placement[3] == 58) {
placement[3] = 7;
} else if (placement[5] == 60) {
placement[5] = 7;
} else if (placement[7] == 62) {
placement[7] = 7;
}
board.setAdapter(adapter);
}
if(!redTurn && ai)
{
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
int[] tiles = {R.drawable.blacktile, R.drawable.redtile, R.drawable.yellowtile,
R.drawable.whitepiece, R.drawable.redpiece, R.drawable.whitepieceselected, R.drawable.redpieceselected,
R.drawable.whiteking, R.drawable.redking, R.drawable.whitekingselected, R.drawable.redkingselected};
public ImageAdapter (Context c) {context=c;}
@Override
public int getCount() {return placement.length;}
@Override
public Object getItem(int position)
{
return tiles[position];
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
pic = new ImageView(context);
pic.setImageResource(tiles[placement[position]]);
pic.setScaleType(ImageView.ScaleType.FIT_CENTER);
pic.setLayoutParams(new GridView.LayoutParams(51,51));
return pic;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mcollins499.mcollins_prog11.MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/Turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Red's Turn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/board" />
<GridView
android:id="@+id/board"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="70dp"
android:layout_marginEnd="42dp"
android:layout_marginStart="42dp"
android:layout_marginTop="105dp"
android:clickable="false"
android:focusable="false"
android:contextClickable="false"
android:numColumns="8"
app:layout_constraintBottom_toTopOf="@+id/Turn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
Для справки о том, что означают значения в размещении:
0 = black tile
1 = red tile
2 = highlighted tile
3 = black/white piece
4 = red piece
5 = highlighted black/white piece
6 = highlighted red piece
7 = black/white king
8 = red king
9 = highlighted black/white king
10 = highlighted red king