Method is not visible to print details on Snakes and Ladders Game - java

public class CA {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] numbersOnBoard = new int[6][6];
boardSetUpA(numbersOnBoard);
printTwoD(numbersOnBoard);
String[][] details = new String[6][6];
boardDetails(details);
}
public static void boardSetUpA(int[][] twoD) {
// Square with even size
// even rows
for (int row = 0; row < twoD.length; row++) {
if (row % 2 == 0) {
int num = twoD.length * (twoD.length - row);
for (int col = 0; col < twoD[row].length; col++) {
twoD[row][col] = num;
num--;
}
} //
else {
int num = twoD.length * (twoD.length - (row + 1)) + 1;
for (int col = 0; col < twoD[row].length; col++) {
twoD[row][col] = num;
num++;
}
}
} // for row
} //
public static void printTwoD(int[][] array) {
for (int row = 0; row < array.length; row++) {
for (int column = 0; column < array[row].length; column++) {
System.out.print(array[row][column] + "\t");
}
System.out.println();
}
}
public static void boardDetails(String[][] board) {
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (col + 2 == row || col + 1 == row * 2) {
board[row][col] = "Lad"; // Append value
} else if (col * 2 == row || row * 2 == col) {
board[row][col] = "Cht"; // Append value
} else {
board[row][col] = " ";
}
}
board[board.length - 1][0] = "Start";
if (board.length % 2 == 0) {
board[0][0] = "End";
} else {
board[0][board.length - 1] = "End";
}
}
}
public static void printBoard(int[][] twoD, String[][] strTwoD) {
// Printing
for (int row = 0; row < twoD.length; row++) {
for (int col = 0; col < twoD[row].length; col++) {
System.out.print(twoD[row][col] + " " + strTwoD[row][col] + "\t\t");
}
System.out.println("\n");
}
}
}
I am creating a snakes and ladders game board and the game (will be finished later).I am trying to print the chutes/snakes and ladders on the board using the method boardDetails but it is not showing up, only the board from 1-36 is shown. Am I not calling the method? Any help would be appreciated.
Thanks!

Related

Sudoku: Valid values in a position

I'm trying to figure how answer to these question in my code:
create a method called getValidValues that: returns an array of 9 boolean values that corresponds to 9 digits (1-9) and, it is true if that digit can be placed in that position [row][column] without violating game rules.
This is my code:
public class SudokuClass {
private final int SIZE = 9;
boolean board = new int[SIZE][SIZE];
boolean[][] start = new boolean[SIZE][SIZE];
public SudokuClass() {
for(int i=0; i < SIZE; i++) {
for(int j=0; j < SIZE; j++) {
board[i][j] = 0;
}
}
}
public String toString () {
String result = "";
for (int i = 0; i < SIZE; i++) {
if (i % 3 == 0) {
result = result + "+-------+-------+-------+\n";
}
for (int j = 0; j < SIZE; j++) {
if (j % 3 == 0) {
result = result + "| ";
}
if (scacchiera [i] [j] == 0) {
result = result + " ";
} else {
result = result + board[i][j] + " ";
}
}
result = result + "|\n";
}
result = result + "+-------+-------+-------+\n";
return result;
}
public void addStartValues(int row,int col, int val) {
board[row][col] = value;
start[row][col] = true;
}
public void addMove(int row,int col,int val) {
scacchiera[row][col] = val;
inizio[row][col] = false;
}
public boolean verifyGame () {
if (board.length != 9) {
System.out.println("Board should have 9 rows");
return false;
}
for (int i = 0; i < board.length; i++) {
if (board[i].length != 9) {
System.out.println("Row "+ i +" should have 9 cells.");
return false;
}
}
/* check each cell for conflicts */
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
int cell = board[i][j];
if (cell == 0) {
continue; /* blanks are always OK */
}
if ((cell < 1) || (cell > 9)) {
System.out.println("Row "+ i +", column "+ j +" has value illegal "+ cell);
return false;
}
/* does it match any other value in the same row? */
for (int m = 0; m < board.length; m++) {
if ((j != m) && (cell == board[i][m]))
{
System.out.println("Row "+ i +" has "+ cell +" in position "+ j +" and "+ m);
return false;
}
}
/* does it match any other value it in the same column? */
for (int k = 0; k < board.length; k++) {
if ((i != k) && (cell == board[k][j])) {
System.out.println("Column "+ j +" has "+ cell +" in position "+ i +" and "+ k);
return false;
}
}
/* does it match any other value in the 3x3? */
for (int k = 0; k < 3; k++) {
for (int m = 0; m < 3; m++) {
int testRow = (i / 3 * 3) + k; /* test this row */
int testCol = (j / 3 * 3) + m; /* test this col */
if ((i != testRow) && (j != testCol) && (cell == board[testRow][testCol])) {
System.out.println("Value "+ cella +" at row "+ i +", column "+ j +" matches with value at row "+ testRow +", column "+ testColumn);
return false;
}
}
}
}
}
return true;
}
public int getValoreIn(int row, int col) {
return scacchiera[row][col];
}
private boolean isInRow(int row, int num) {
for (int i = 0; i < SIZE; i++)
if (board[row][i] == num) {
return true;
}
return false;
}
// we check if a possible number is already in a column
private boolean isInCol(int col, int number) {
for (int i = 0; i < SIZE; i++)
if (board[i][col] == number) {
return true;
}
return false;
}
// we check if a possible number is in its 3x3 box
private boolean isInBox(int row, int col, int number) {
int r = row - row % 3;
int c = col - col % 3;
for (int i = r; i < r + 3; i++)
for (int j = c; j < c + 3; j++)
if (board[i][j] == number) {
return true;
}
return false;
}
public boolean[][] getValidValues(int row, int col) {
boolean[][] validValues = new boolean[9][9];
int[] digit = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for(int i=0; i < digit.length; i++) {
for(int j=0; j < digit.length; j++) {
if(!isInRow(row,digit[i]) && !isInCol(col,digit[i]) && !isInBox(row,col,digit[i])) {
validValues[i][j] = true;
} else {
validValues[i][j] = false;
}
}
}
return validValues;
}
I edited the code, adding other, private, methods called: isInRow, isInCol, isInBox. I thought to do this to get an easier way to implement the method getValidValues. What do you think about? Are there any suggestions?
The main rule in sudoku is: all numbers in a column, a row and a 3x3 square have to be unique. Based on that you have to do three thing:
Iterate over all cells in the same column. If given column contains a number, set that number to invalid.
Same as above but for the row.
Find out the 3x3 square for the cell you're validating. It will start at coordinates like [floor(x/3), floor(y/3)]. Then you iterate over cells in that square and set numbers to invalid, just like above.
I hope that is enough to get you started. Don't want to post the code because that will take away the learning process.

How to find unique elements in a two-dimensional array of strings in java?

I have a two-dimensional array of String. This is a Matrix. I need to sort this Matrix and save unique items in first line other Matrix.How to do this use only own arlgorithm.I mean do not call a method but write the loop itself that will sort through and compare the elements of the array
import java.util.Scanner;
public class Coursework {
public static void main(String[] args) {
final int linesOfMatrix; //number of lines in the matrix
System.out.println("Enter number of lines: ");
Scanner sc = new Scanner(System.in);
linesOfMatrix = sc.nextInt();
Scanner sc2 = new Scanner(System.in);
String [][] matrix = new String [linesOfMatrix][]; // declare the Matrix
for(int i=0; i < matrix.length; i++) {
System.out.println("Enter a value for the string " + (i+1) + "
through a space");
matrix[i] = sc2.nextLine().split(" ");
}
sc.close();
sc2.close();
//below must be unique sort, but he dosen't work rigth
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length-1; j++){
if(matrix[i][j].equals(matrix[i][j+1])){
matrix[i][j+1] = matrix[i][j+1];
}
}
}
System.out.println("Matrix");
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length-1; j++){
System.out.println("[" +(i) + "][" + (j) + "]= " + matrix[i]
[j] + " [" + (i) + "][" + (j+1) + "]= " + matrix[i][j+1] );
}
}
}
}
What about using Map with counting elements:
public static String[] getUnique(String[][] matrix) {
Map<String, Integer> map = new LinkedHashMap<>();
for (String[] row : matrix)
for (String col : row)
map.put(col, map.getOrDefault(col, 0) + 1);
List<String> unique = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet())
if (entry.getValue() == 1)
unique.add(entry.getKey());
return unique.toArray(new String[unique.size()]);
}
In case you do not want to use Map, then you coudl just do the same with a bit slower:
public static String[] getUnique(String[][] matrix) {
List<String> unique = new ArrayList<>();
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] == null)
continue;
boolean foundUnique = true;
for (int i = row; i < matrix.length; i++) {
for (int j = i == row ? col : 0; j < matrix[i].length; j++) {
if (matrix[i][j] == null || (i == row && j == col))
continue;
if (matrix[i][j].equals(matrix[row][col])) {
foundUnique = false;
matrix[i][j] = null;
}
}
}
if (foundUnique)
unique.add(matrix[row][col]);
else
matrix[row][col] = null;
}
}
return unique.toArray(new String[unique.size()]);
}
Or even do not use List :-):
public static String[] getUnique(String[][] matrix) {
int total = 0;
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] == null)
continue;
boolean foundUnique = true;
for (int i = row; i < matrix.length; i++) {
for (int j = i == row ? col : 0; j < matrix[i].length; j++) {
if (matrix[i][j] == null || (i == row && j == col))
continue;
if (matrix[i][j].equals(matrix[row][col])) {
foundUnique = false;
matrix[i][j] = null;
}
}
}
if (foundUnique)
total++;
else
matrix[row][col] = null;
}
}
if (total == 0)
return new String[0];
String[] res = new String[total];
for (int row = 0, i = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[row].length; col++)
if (matrix[row][col] != null)
res[i++] = matrix[row][col];
return res;
}

Tic Tac Toe / Java

I've done my first aplication - Tic Tac Toe. I want to add a new function - display a winner (player X or O). How Can I do it?
public class Test extends JFrame {
int counter = 0;
public Test(){
setSize(800,800);
setTitle("Kółko i krzyżyk");
setVisible(true);
setLayout(new GridLayout(3,3));
for (int i = 1; i <= 9; i++){
JButton button = new JButton ("");
add (button);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (counter % 2 ==0){
button.setText("X");
System.out.println("X");
} else{
button.setText("O");
System.out.println("O");
}
button.setEnabled(false);
counter++;
}
});
}
}
1.Player Class :
import java.util.Scanner;
public class Player {
private String name;
private MarkType mark;
private int row, col;
Scanner sc = new Scanner(System.in);
public Player(String name, MarkType mark) {
this.mark = mark;
this.name = name;
}
public String getName() {
return name;
}
public MarkType getMark() {
return mark;
}
public void nextMove() {
System.out.println(this.getName() + " enter position for " + this.getMark());
row = sc.nextInt();
col = sc.nextInt();
}
public int getRow() {
return row - 1;
}
public int getCol() {
return col - 1;
}
}
2.Board Class :
public class Board {
private MarkType board[][];
private int bsize;
public Board(int bsize) {
this.bsize = bsize;
board = new MarkType[bsize][bsize];
}
public int getBoardSize() {
return bsize;
}
public void updateBoard(int row, int col, MarkType mark) {
if (row > bsize || col > bsize) {
throw new ArrayIndexOutOfBoundsException();
} else if (board[row][col] == MarkType.$) {
board[row][col] = mark;
} else {
throw new InvalidPosition("Enter valid position");
}
}
public void initialize() {
for (int row = 0; row < bsize; row++)
for (int col = 0; col < bsize; col++)
board[row][col] = MarkType.$;
}
public boolean isWinner(int passedRow, int passedCol, MarkType mark) {
for (int col = 0; col < bsize; col++) {
if (board[passedRow][col] != mark)
break;
if (col == bsize - 1) {
return true;
}
}
for (int row = 0; row < bsize; row++) {
if (board[row][passedCol] != mark)
break;
if (row == bsize - 1) {
return true;
}
}
if (passedRow == passedCol) {
for (int row = 0; row < bsize; row++) {
if (board[row][row] != mark)
break;
if (row == bsize - 1) {
return true;
}
}
}
if (passedRow + passedCol == bsize - 1) {
for (int index = 0; index < bsize; index++) {
if (board[index][(bsize - 1) - index] != mark)
break;
if (index == bsize - 1) {
return true;
}
}
}
return false;
}
public void display() {
for (int row = 0; row < bsize; row++) {
for (int col = 0; col < bsize; col++) {
System.out.print(" " + board[row][col] + " ");
if (col != bsize - 1) {
System.out.print("|");
}
}
System.out.println();
if (row != bsize - 1) {
for (int col = 0; col < bsize; col++)
System.out.print("--- ");
}
System.out.println();
}
}
}
for complete answer follow the link below:
http://programmersthing.blogspot.in/2017/06/simple-game-in-java.html

Generating an unique Sudoku

I am making a Sudoku game in Java and I need some help.
I've got two classes for generating the Sudoku puzzle: SudokuSolver, SudokuGenerator`.
The SudokuSolver creates a full valid Sudoku puzzle for an empty table and the SudokuGenerator removes a random value from the table and then checks (using SudokuSolver) if the Sudoku puzzle is unique.
I found out that the Sudoku puzzle isn't unique, so I think my algorithm for checking the uniqueness of the Sudoku puzzle is bad.
For example, I have this Sudoku puzzle
497816532
132000000
000000000
910600080
086009000
000084963
021063059
743050020
600278304
And this solution:
497816532
132**745**698
568392471
914**637**285
386529147
275184963
821463759
743951826
659278314
I went to https://www.sudoku-solutions.com/ and I added the pattern of my Sudoku and they gave me another solution:
497816532
132**547**698
568392471
914**635**287
386729145
275184963
821463759
743951826
659278314
The funny think is that they're saying that This puzzle is valid and has a unique solution.
Some opinions?
SudokuSolver
public class SudokuSolver {
public static final int GRID_SIZE = 9;
public static final int SUBGRID_SIZE = 3;
private static int validRow = 0;
private static int validCol = 0;
private int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
public boolean solveSudoku(int[][] values, int forbiddenNum) {
if(!findUnassignedLocation(values)) return true;
//suffle the nums array - for having a different valid sudoku
shuffleNums();
for (int i = 0; i < GRID_SIZE; i++) {
int num = nums[i];
if(num == forbiddenNum) continue; //
if(isSafe(values,validRow, validCol, num)) {
values[validRow][validCol] = num;
if(solveSudoku(values, forbiddenNum)) return true;
if(validCol == 0) {
validRow--;
validCol = 8;
}else{
validCol--;
}
values[validRow][validCol] = 0;
}
}
return false;
}
public boolean createValidSudoku(int[][] values) {
if(!findUnassignedLocation(values)) return true;
shuffleNums();
for (int i = 0; i < GRID_SIZE; i++) {
int num = nums[i];
if(isSafe(values,validRow, validCol, num)) {
values[validRow][validCol] = num;
if(createValidSudoku(values)) return true;
if(validCol == 0) {
validRow--;
validCol = 8;
}else{
validCol--;
}
values[validRow][validCol] = 0;
}
}
return false;
}
private void shuffleNums() {
Random random = new Random();
for(int i = nums.length - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
int a = nums[index];
nums[index] = nums[i];
nums[i] = a;
}
}
private boolean findUnassignedLocation(int[][] values) {
for(int row = 0; row < GRID_SIZE; row++) {
for(int col = 0; col < GRID_SIZE; col++) {
if (values[row][col] == 0) {
validRow = row;
validCol = col;
return true;
}
}
}
return false;
}
private boolean usedInRow(int[][] values, int row, int num) {
for (int col = 0; col < GRID_SIZE; col++) {
if(values[row][col] == num) return true;
}
return false;
}
private boolean usedInCol(int[][] values, int col, int num) {
for (int row = 0; row < GRID_SIZE; row++) {
if(values[row][col] == num) return true;
}
return false;
}
private boolean usedInBox(int[][] values, int boxStartRow, int boxStartCol, int num) {
for(int row = 0; row < SUBGRID_SIZE; row++) {
for (int col = 0; col < SUBGRID_SIZE; col++) {
if (values[row + boxStartRow][col + boxStartCol] == num) return true;
}
}
return false;
}
private boolean isSafe(int[][] values,int row, int col, int num) {
return !usedInRow(values, row, num) &&
!usedInCol(values, col, num) &&
!usedInBox(values, row - row % 3, col - col % 3, num);
}
public void printGrid(int[][] values) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
System.out.print(values[row][col]);
}
System.out.println();
}
}
}
SudokuGenerator
public class SudokuGenerator {
private int[][] generatorValues = new int[9][9];
public void generateSudoku() {
SudokuSolver sudokuSolver = new SudokuSolver();
//generate a random valid sudoku for an empty table
sudokuSolver.createValidSudoku(generatorValues);
int count = 0;
printNums(generatorValues);
while(count < 40){
Random random = new Random();
int row = 0;
int col = 0;
if(count < 15){
row = random.nextInt(3);
col = random.nextInt(9);
} else if (count >= 15 && count < 30) {
row = random.nextInt(3) + 3;
col = random.nextInt(9);
}else {
row = random.nextInt(3) + 6;
col = random.nextInt(9);
}
int num = generatorValues[row][col];
int tempValues[][] = Arrays.copyOf(generatorValues, generatorValues.length);
//System.out.println("Row:" + row + "Col: " + col + "Num: " + num);
//Set the cell to 0;
if(generatorValues[row][col] != 0){
generatorValues[row][col] = 0;
} else{
continue;
}
//If found a solution, set cell back to original num
if(sudokuSolver.solveSudoku(tempValues, num)) {
generatorValues[row][col] = num;
continue;
}
count++;
}
System.out.println("------------------");
printNums(generatorValues);
}
private void printNums(int[][] values) {
for (int row = 0; row < 9; row++) {
for(int col = 0; col < 9; col++) {
System.out.print(values[row][col]);
}
System.out.println();
}
}
public int[][] getGeneratorValues () {
return generatorValues;
}
}

Arrays and 'null' can I remove it and will it affect a random number generator

I'm just wondering is there a way to make the 'null' that is outputted when using a for loop output white space?
For example, I have a 2d array of an type object and when displaying the array I want to display the first three elements and then skip say 10 empty spaces then print another three elements.
Also if I was to use a random generator to pick array elements at random would the empty elements affect this or not?
Main:
public class CinemaSystem {
public static void main(String[] args) {
Seat cinemaactual = new Seat("Cinema");
Seat[][] cinema = new Seat[12][23];
Ticket ticket = new Ticket();
Scanner scan = new Scanner(System.in);
String answer, contiune;
int number, check = 0, category, id;
do {
System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
+ "\nWould you like to purchase tickets or list available seats?"
+ "(/Purchase/List/Help)");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("purchase")) {
do {
System.out.println("Please choose the cateogry of ticket "
+ "you would like, followed by who the ticket is for"
+ "and the amount required. (separated by a space)\n"
+ "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
+ " Child\n3. Concession");
category = scan.nextInt();
number = scan.nextInt();
id = scan.nextInt();
if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {
ticket.SetType(category);
if (category == 1) {
ticket.SetName("Gold");
} else if (category == 2) {
ticket.SetName("Siler");
} else {
ticket.SetName("Bronze");
}
ticket.SetNumber(number);
ticket.SetID(id);
if (id == 1) {
ticket.SetCategory("Adult");
} else if (id == 2) {
ticket.SetCategory("Child");
} else {
ticket.SetCategory("Bronze");
}
System.out.print("You have selected"
+ ticket.GetNumber() + " " + ticket.GetName()
+ " ticket(s) at the" + ticket.GetCategory() + " price .");
ticket.BuyTicket(category, id, number);
} else {
System.out.print("Sorry, incorrect input, please enter an apropriate value.");
check = scan.nextInt();
}
} while (check == 0 || check > 3);
do {
System.out.print("Would you like to perchase more tickets? (Yes/No)");
contiune = scan.nextLine();
} while (contiune.equalsIgnoreCase("Yes"));
} else if (answer.equalsIgnoreCase("list")) {
cinemaactual.CreateTheatre(cinema);
cinemaactual.DisplayTheatre(cinema);
} else if (answer.equalsIgnoreCase("help")) {
// Code for help
} else if (answer.equalsIgnoreCase("quit")) {
System.exit(-1);
}
System.out.print("Sorry, incorrect input please enter"
+ " a valid input (Purchase/List/Help or QUIT to exit");
answer = scan.nextLine();
} while (!answer.equalsIgnoreCase("purchase")
|| !answer.equalsIgnoreCase("List")
|| !answer.equalsIgnoreCase("help")
|| !answer.equalsIgnoreCase("quit"));
}
}
Method in the class that makes the array:
public Seat[][] CreateTheatre(Seat[][] x) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row < 12; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 0; row < 8; row++) {
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row <12; row++){
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 4; col < 9; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 3; row < 5;row++){
for (int col = 14; col < 19; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 5; row < 9; row++) {
for (int col = 4; col < 9; col++){
x[row][col] = new Seat("S");
}
}
for (int row = 5; row < 9; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 6; row < 9; row++) {
for (int col = 9; col < 14; col++) {
x[row][col] = new Seat("G");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 16; col++) {
x[row][col] = new Seat("G");
}
}
return x;
}
For the display of the array:
public void DisplayTheatre(Seat[][] x) {
for (int row = 0; row < x.length; row++) {
for (int col = 0; col < x[row].length; col++) {
System.out.print(x[row][col] + " ");
}
System.out.println();
}
}
Just wondering if I can remove the null since I want some objects display then blank space in the middle and then some more, on the same line.
Also do null elements interfere with randomly selecting elements from an array, like will a null element ever be randomly selected?
if i understand your requirement. below is something you are looking for .
String[] arr = new String[5];
arr[0]="2";
arr[1]="3";
arr[4]="5";
for(int i=0; i<arr.length; i++){
if(arr[i]==null){
System.out.println(' ');
}
else {
System.out.println(arr[i]);
}
}
Also if I was to use a random generator to pick array elements at random would the empty elements affect this or not?
If some of the elements were null, then picking an element at random would inevitably give you a null sometimes.
You could attempt to deal with this by trying again with a new random number ... until you get a non-null element. But that could be expensive if the array is sparse. And it could give you an infinite loop if all of the elements are null.
(But don't just search from the initial random position for a non-null element ... because that will give a biased selection.)
However a better solution would be to have a separate list containing just the unoccupied seats (or whatever).
And an ideal cinema booking system would not allocate seats at random. Rather, it would try to fill methodically ... so that Jim and his girlfriend have a good chance of sitting together even if they buy the last 2 tickets.

Categories

Resources