Question

Reposting question with additional information. I am writing a java program that has the computer play...

Reposting question with additional information.

I am writing a java program that has the computer play a game of ConnectFour with the user, I am having trouble with the code that tells who wins the game. when I play the game after getting four in a row the program just continues to go on. I believe my FindLocalWinner method should be correct, so I need help with the findWinner method.

/**
* Check for a win starting at a given location and heading in a
* given direction.
*
* Returns the char of the player with four in a row starting at
* row r, column c and advancing rowOffset, colOffset each step.
* Returns NONE if no player has four in a row here, or if there
* aren't four spaces starting here.
*
* For example, if rowOffset is 1 and colOffset is 0, we would
* advance the row index by 1 each step, checking for a vertical
* win. Similarly, if rowOffset is 0 and colOffset is 1, we would
* check for a horizonal win.
* @param board The game board.
* @param r Row index of where win check starts
* @param c Column index of where win check starts
* @param rowOffset How much to move row index at each step
* @param colOffset How much to move column index at each step
* @return char of winner from given location in given direction
* or NONE if no winner there.
*/
public static char findLocalWinner(char[][] board, int r, int c,
int rowOffset, int colOffset) {
// TODO You have to write this.
char player = board[r][c];
if(player == NONE) {
   //player = NONE;
   return NONE;
}
for (int i = 0; i < 4; i++ ) {
int col = c+i*colOffset;
int row = r+i*rowOffset;
if(player != NONE || c > COLUMNS || c < 0 || r > ROWS || r < 0) {
  
return NONE;
}if (board[row][col] != player) {
return NONE;
}

  
  
}

   return player;
}
/**
* Checks entire board for a win (4 in a row).
*
* @param board The game board.
* @return char (HUMAN or COMPUTER) of the winner, or NONE if no
* winner yet.
*/
public static char findWinner(char[][] board) {
// TODO You have to write this.
// HINT: You should call the findLocalWinner method to help you
//douyble for look watch out for rowoffset
//for(int i = 0; i < COLUMNS; i ++) {

//}
char Winner = NONE;
for(int row = 0; row < ROWS; row++) {
for(int col = 0; col < COLUMNS; col++) {
if(Winner == NONE) { //makes sure winner is NONE unless it finds human or comp
Winner = findLocalWinner(board,row,col,0,1);
}if(Winner == NONE) {
Winner = findLocalWinner(board,row,col,1,0);
}if(Winner == NONE) {
Winner = findLocalWinner(board,row,col,1,1);
}if(Winner == NONE) {
Winner = findLocalWinner(board,row,col,-1,1);
}
}
}
return Winner;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static char findWinner(char[][] board) {
        
        // check for a row.
        for(int row = 0; row < ROWS; row++) {
                for(int col = 0; col < COLUMNS - 3; col++) {
                        char w = findLocalWinner(board, row, col, 0, 1);
                        if(w != NONE) {
                                return w;
                        }
                }
        }
        
        // check for a column.
        for(int row = 0; row < ROWS - 3; row++) {
                for(int col = 0; col < COLUMNS; col++) {
                        char w = findLocalWinner(board, row, col, 1, 0);
                        if(w != NONE) {
                                return w;
                        }
                }
        }
        
        // check for a major diagonal. (top left to bottom right)
        for(int row = 0; row < ROWS - 3; row++) {
                for(int col = 0; col < COLUMNS - 3; col++) {
                        char w = findLocalWinner(board, row, col, 1, 1);
                        if(w != NONE) {
                                return w;
                        }
                }
        }
        
        // check for a minor diagonal. (bottom left to top right)
        for(int row = ROWS - 1; row >= 3; row++) {
                for(int col = 0; col < COLUMNS - 3; col++) {
                        char w = findLocalWinner(board, row, col, -1, 1);
                        if(w != NONE) {
                                return w;
                        }
                }
        }
        
        return NONE;
}
Add a comment
Know the answer?
Add Answer to:
Reposting question with additional information. I am writing a java program that has the computer play...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • I'm writing a program in java called Sokoban. I'm pretty far in, but there are a...

    I'm writing a program in java called Sokoban. I'm pretty far in, but there are a few methods that I have no idea where to go from where I am now. Method 1: /**    * Moves a box on the board.    *    * Step 1: Use your checkDelta method to check that the move is valid. Recall that there are 2    * characters that can represent a box. Step 2: Use your togglePos method to correctly...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I've created a functioning program but I can't figure out how to implement class structure into...

    I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you. #pragma once #include <iostream> using namespace std;    bool DeclareResults(char ch, char gameboard[][3]);    bool FilledBoard(char gameboard[][3]);    void printGameBoard(char gameboard[][3]); #include "fnt.h"; #include <iostream> using namespace std; int main() {    //declare...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT