Need help with a 2D list - I'm almost there!
#Write a function called check_winner which takes
#as input a 2D list. It should return "X" if there are four
#adjacent "X" values anywhere in the list (row, column,
#diagonal); "O" if there are four adjacent "O" values
#anywhere in the list; and None if there are neither.
#
#Here are the ways Connect-4 is different from tic-tac-toe:
#
# - Connect-4 is played with 6 rows and 7 columns
# - You must have 4 in a row (or column or diagonal) to win
# - You may only place pieces in the bottom-most empty
# space in a column (e.g. you "drop" the pieces in the
# column and they fall to the first empty spot). Note,
# though, that this shouldn't affect your reasoning.
#
#We'll use "X" and "O" to represent the players, and
#None to represent empty spots.
#Assume there will be only one winner per board,
#no characters besides "X", "O", and None.
#Ignore whether the board is actually a valid game
# of Connect 4.
#
#Hints:
# - Check both kinds of diagonals, top-left to
# bottom-right and bottom-left to top-right.
# - This board is too large to check every possible place
# for a winner: there are 69 places a player could win.
# - Remember, if you put a negative index in a list,
# Python "wraps around" and checks the last value. You
# may have to control for this.
#Write your function here!
def is_list_identical(l):
return len(set(l)) <= 1
def check_winner(board):
rows = len(board)
cols = len(board[0])
# find rows containing 4 values together.
for row in board:
l = len(row)
for i in range(l-3):
if is_list_identical(row[i:i+4]):
return row[i]
# find cols containing 4 values together.
for c in range(cols):
col = [board[x][c] for x in range(rows)]
l = len(col)
for i in range(l-3):
if is_list_identical(col[i:i+4]):
return col[i]
# find primary diagonal (top left to bottom right)
for r in range(rows-3):
for c in range(cols-3):
data = [board[r+x][c+x] for x in range(4)]
if is_list_identical(data):
return data[0]
# find secondary diagonal (top right to bottom left)
for r in range(rows-3):
for c in range(cols-1, 3, -1):
data = [board[r+x][c-x] for x in range(4)]
if is_list_identical(data):
return data[0]
return None
#The code below tests your function on three Connect-4
#boards. Remember, the line breaks are not needed to create
#a 2D tuple; they're used here just for readability.
xwins = ((None, None, None, None, None, None, None),
(None, None, None, None, None, None, None),
(None, None, None, None, "X" , None, None),
(None, None, None, "X" , "O" , "O", None),
(None, "O" , "X" , "X" , "O" , "X", None),
("O" , "X" , "O" , "O" , "O" , "X" , "X"))
owins = ((None, None, None, None, None, None, None),
(None, None, None, None, None, None, None),
("O" , "O" , "O" , "O" , None, None, None),
("O" , "X" , "X" , "X" , None, None, None),
("X" , "X" , "X" , "O" , "X" , None, None),
("X" , "O" , "O" , "X" , "O" , None, None))
nowins =(("X" , "X" , None, None, None, None, None),
("O" , "O" , None, None, None, None, None),
("O" , "X" , "O" , "O" , None, "O" , "O" ),
("O" , "X" , "X" , "X" , None, "X" , "X" ),
("X" , "X" , "X" , "O" , "X" , "X" , "O" ),
("X" , "O" , "O" , "X" , "O" , "X" , "O" ))
print(check_winner(xwins))
print(check_winner(owins))
print(check_winner(nowins))
Need help with a 2D list - I'm almost there! #Write a function called check_winner which...
can someone indent this code correctly in python programming def count_neighbors(cells,row,col): rows=len(cells) #storing no. of rows cols=len(cells[0]) #storing no. of columns if(row<0 or col<0 or row>rows-1 or col>cols-1): # when row or column is out of range return -1 count=0 if(row==0 and cells[rows-1][col]==1): #cyclic order count+=1 if(col==0 and cells[row][cols-1]==1): count+=1 if(row==rows-1 and cells[0][col]==1): count+=1 if(col==cols-1 and cells[row][0]==1): count+=1 if(col>=1 and cells[row][col-1]==1): #left neighbor count+=1 if (row>=1 and cells[row-1][col]==1): #upper neighbor count+=1 if(row+1<rows and cells[row+1][col]==1): #down neighbor count+=1 if(col+1<cols and cells[row][col+1]==1):...
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...
Throughout this script, can you provide helpful comments about how each function works in the script plus it's significance to the alignment process. Also are there any errors in this script or a way to improve this script? Any help would be appreciated. Thank you. THIS IS A PYTHON CODE AND IS IN IT'S FORMAT _ ITS CLEAR! #!/usr/bin/env python # file=input("Please provide fasta file with 2 sequences:") match=float(input('What is the match score?:')) missmatch=float(input('What is the missmatch score?:')) gap=float(input('What is...
2D Lists + File I/O In a comma-separated input file named results.txt, you have been given the following information that records the weekly (movie) box office sales for 5 movies. A sample input file will include the following. The movie’s title is listed first, then its sales (in million dollars) for 7 days are listed. Avengers,169.1,125.8,101.7,40.5,38.2,24.2,55.7 Shazam!,8.6,14.1,8.2,7.3,31.4,44.2,26.8 Breakthrough,14.8,16.1,18.0,18.9,19.8,21.8,24.6 The Best of Enemies,4.7,5.4,5.8,6.1,6.7,7.6,8.1 Dumbo,9.9,14.8,9.0,7.9,40.6,52.5,36.3 Write a complete Python program that includes code to do the following: read in the data from...
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)<<"...
Write a function called ZeroCornersSecondRow that sets the corners of a 2D array to zero and also outputs the second row. The input arguments: • inArray: A double precision 2D array of size nxn, where n is at least 3. The output arguments: outArray: A double precision 2D array of size nxn that is a copy of inArray except with the corners set to zero. • secondRow: A double precision 1D array of size n. This is the second row...
In problem 3, you will write a function, findvertically (), which will determine whether or not a given word exists in a word search puzzle vertically. In word search puzzles, words can be written upwards or downwards. For example, "BEAK" appears at [1] [3] written downwards, while "BET" appears at [2] [2] written upwards: [["C", "A", "T", "X", "R"], ["D", "T", "E", "B", "L"], ["A" "R" "B", "E", "Z"], ["X", "O", "E", "A", "U"], ["J", "S", "O", "K", "W"]] Your...
PLEASE COMPLETE IN C++ LANGUAGE
Location row : int = -1 col : int = -1 setLocation(row : int, col : int) getRow(): int getColl): int isEmpty(): bool Details Write all the code necessary to implement the Location class as shown in the UML Class Diagram to the right. Do this in a project named snake (we'll continue adding files to this project as the term progresses). Your class definition and implementation should be in separate files. When complete, you...
please explain/ comment
3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...
Please use Python 3, thank you~ Write a program that: Defines a function called find_item_in_grid, as described below. Calls the function find_item_in_grid and prints the result. The find_item_in_grid function should have one parameter, which it should assume is a list of lists (a 2D list). The function should ask the user for a row number and a column number. It should return (not print) the item in the 2D list at the row and column specified by the user. The...