How to Fix a “Pit Cannot Be Resolve to a Type” Error in My Android Mancala Game

I’m currently building a Mancala based board game for Android, and while defining the game logic, I stumbled upon a frustrating error in Eclipse:

"Pit cannot be resolved to a type"

This error popped up when I tried to instantiate my pit objects with:

Player1Pits[i] = new Pit(i, _pitContents);

At first glance, this looked totally valid. I assumed I was correctly defining a pit by assigning it an index and a starting stone count. But Java clearly disagreed and that forced me to take a closer look at my code structure.

Explanation of the Error

After digging into the issue, I realized the mistake was simple but critical: I defined Pit as a method, not a class. In Java, when you use new Pit(...), you’re telling the compiler to instantiate a class object, not call a method.

Here’s the buggy code I had:

public int Pit(int pitIndex, int pitContents) { 
this.pitIndex = pitIndex;
this.pitContents = pitContents;
}

Here, Pit is treated as a regular method that returns an int. Java can’t interpret this as a constructor or class, which is why Eclipse threw the error.

The Solution

To fix this, I needed to define Pit as a proper Java class, complete with a constructor, variables, and optional methods to manage the stones. I could define it as either an inner class inside Game, or a separate file altogether. I opted for the inner class approach for now to keep things tightly coupled during early development.

Fix and Improve Code

Here’s the revised and working version of my code with added functionality for a better gameplay experience:

public class Game extends Activity {

int _pitContents = 4;
int pitsPerRow = 5;
TableView tableView;

// define Pit class
public class Pit {
public int index;
public int contents;

public Pit(int index, int contents) {
this.index = index;
this.contents = contents;
}

public void addStone() {
contents++;
}

public void removeStone() {
if (contents > 0) {
contents--;
}
}

public int getStoneCount() {
return contents;
}
}

// Array of pits for player 1
public Pit[] player1Pits = new Pit[2 * pitsPerRow];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// initialize table view
tableView = new TableView(this);
setContentView(tableView);
tableView.requestFocus();

// initialize pits
for (int i = 0; i < (2 * pitsPerRow); i++) {
if (i < pitsPerRow) {
player1Pits[i] = new Pit(i, _pitContents);
} else {
// empty pit or storage
player1Pits[i] = new Pit(i, 0);
}
}
}
}

Added Practice Functionality

While rewriting the code, I also added some helpful methods inside the Pit class:

  1. addStone() and removeStone() — These allow me to simulate turns and movements easily during gameplay.
  2. Proper use of onCreate() and TableView — Ensuring my UI initializes correctly.
  3. Expandable object design — Now I can easily add features like capture logic or animations without refactoring the core structure.

Further Suggestion

As my project evolves, I plan to:

  • Create a Board class to manage the overall game state.
  • Implement game logic like stone distribution, turn switching, and game over detection.
  • Use TableView to visually display pit states, maybe even animate stone movement.

This structure makes the code modular, readable, and easier to scale for multiplayer logic or AI support later.

Final Thought

This bug reminded me how important it is to truly understand object-oriented design in Java especially when building Android games. Defining classes properly is not just a formality; it’s the backbone of how Java apps come together. Now that my Pit class is correctly implemented, my game logic feels much more organized, and I can keep building out the features I envisioned for my Mancala game.

Related blog posts