Connect 4 trouble with the vertical winning condition
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I can't seem to figure out on how I should get my vertical move to work
My board is a nested list consisting of 6 lists with 6 elements in each.
I've been able to figure out my horizontal as well as my diagonal winning condition. But I can't wrap my head around the vertical one
This is my horizontal condition for example
#the list itself
board = [
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
]
def hor_condition(board, player):
"""the hor_condition checks whether the list contains necessary amount of checkers in order to call it a win"""
for row in board:
for cell in row:
if row.index(cell) < 2:
check_list = [row[row.index(cell)],
row[row.index(cell) + 1],
row[row.index(cell) + 2],
row[row.index(cell) + 3]
]
if check_list[0] == player and
check_list[1] == player and
check_list[2] == player and
check_list[3] == player:
return True
This here checks whether there are 4 of the same elements in the same row by a player in order to call it a win.
What I expect the vertical condition to do is to check if there are 4 of the same checkers in the same column
It might be painful to look at, but bear in mind that I've just started with python and I'm beginning to understand the basics.
I do not really ask for black on white code, rather a push in the right direction
I'd be glad to provide any further information if necessary! Thank you in advance!
Edit:
This is a bit late, but I would like to help anyone else who has had the same problem as I did. I eventually fixed my issue with the following:
def vert_condition(board, player):
"""The vert_condition iterates over the board and checks whether 4 vertical slots contain the same player input by
with the help of a nested loop"""
for row in range(3):
for col in range(6):
if (board[row][col] ==
board[row + 1][col] ==
board[row + 2][col] ==
board[row + 3][col] == player):
return True
python list if-statement
|
show 1 more comment
I can't seem to figure out on how I should get my vertical move to work
My board is a nested list consisting of 6 lists with 6 elements in each.
I've been able to figure out my horizontal as well as my diagonal winning condition. But I can't wrap my head around the vertical one
This is my horizontal condition for example
#the list itself
board = [
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
]
def hor_condition(board, player):
"""the hor_condition checks whether the list contains necessary amount of checkers in order to call it a win"""
for row in board:
for cell in row:
if row.index(cell) < 2:
check_list = [row[row.index(cell)],
row[row.index(cell) + 1],
row[row.index(cell) + 2],
row[row.index(cell) + 3]
]
if check_list[0] == player and
check_list[1] == player and
check_list[2] == player and
check_list[3] == player:
return True
This here checks whether there are 4 of the same elements in the same row by a player in order to call it a win.
What I expect the vertical condition to do is to check if there are 4 of the same checkers in the same column
It might be painful to look at, but bear in mind that I've just started with python and I'm beginning to understand the basics.
I do not really ask for black on white code, rather a push in the right direction
I'd be glad to provide any further information if necessary! Thank you in advance!
Edit:
This is a bit late, but I would like to help anyone else who has had the same problem as I did. I eventually fixed my issue with the following:
def vert_condition(board, player):
"""The vert_condition iterates over the board and checks whether 4 vertical slots contain the same player input by
with the help of a nested loop"""
for row in range(3):
for col in range(6):
if (board[row][col] ==
board[row + 1][col] ==
board[row + 2][col] ==
board[row + 3][col] == player):
return True
python list if-statement
Please explain what is your real problem.
– Mr Alihoseiny
Nov 16 '18 at 18:15
I can't come up with a beginning for the vertical condition
– CCG
Nov 16 '18 at 18:16
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
Nov 16 '18 at 18:16
@Prune I wish I could comply but I would not know what else to share. As I tried to explain, I wanted to get an idea on how to tackle a function which checks if the condition ( in this case 4 elements of the same player in one column one after each other ) has been met.
– CCG
Nov 16 '18 at 18:23
You've simply dropped some cognate code on us without any demonstration of your data structures, and none of the required instrumentation for us to test a suggestion or solution. I have little idea what acell
is, that you have to locate another instance withindex
. I don't know why2
is the boundary. I don't understand the repeated calls. As the posting guidelines tell you, please make it easy for us to help you.
– Prune
Nov 16 '18 at 18:23
|
show 1 more comment
I can't seem to figure out on how I should get my vertical move to work
My board is a nested list consisting of 6 lists with 6 elements in each.
I've been able to figure out my horizontal as well as my diagonal winning condition. But I can't wrap my head around the vertical one
This is my horizontal condition for example
#the list itself
board = [
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
]
def hor_condition(board, player):
"""the hor_condition checks whether the list contains necessary amount of checkers in order to call it a win"""
for row in board:
for cell in row:
if row.index(cell) < 2:
check_list = [row[row.index(cell)],
row[row.index(cell) + 1],
row[row.index(cell) + 2],
row[row.index(cell) + 3]
]
if check_list[0] == player and
check_list[1] == player and
check_list[2] == player and
check_list[3] == player:
return True
This here checks whether there are 4 of the same elements in the same row by a player in order to call it a win.
What I expect the vertical condition to do is to check if there are 4 of the same checkers in the same column
It might be painful to look at, but bear in mind that I've just started with python and I'm beginning to understand the basics.
I do not really ask for black on white code, rather a push in the right direction
I'd be glad to provide any further information if necessary! Thank you in advance!
Edit:
This is a bit late, but I would like to help anyone else who has had the same problem as I did. I eventually fixed my issue with the following:
def vert_condition(board, player):
"""The vert_condition iterates over the board and checks whether 4 vertical slots contain the same player input by
with the help of a nested loop"""
for row in range(3):
for col in range(6):
if (board[row][col] ==
board[row + 1][col] ==
board[row + 2][col] ==
board[row + 3][col] == player):
return True
python list if-statement
I can't seem to figure out on how I should get my vertical move to work
My board is a nested list consisting of 6 lists with 6 elements in each.
I've been able to figure out my horizontal as well as my diagonal winning condition. But I can't wrap my head around the vertical one
This is my horizontal condition for example
#the list itself
board = [
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
]
def hor_condition(board, player):
"""the hor_condition checks whether the list contains necessary amount of checkers in order to call it a win"""
for row in board:
for cell in row:
if row.index(cell) < 2:
check_list = [row[row.index(cell)],
row[row.index(cell) + 1],
row[row.index(cell) + 2],
row[row.index(cell) + 3]
]
if check_list[0] == player and
check_list[1] == player and
check_list[2] == player and
check_list[3] == player:
return True
This here checks whether there are 4 of the same elements in the same row by a player in order to call it a win.
What I expect the vertical condition to do is to check if there are 4 of the same checkers in the same column
It might be painful to look at, but bear in mind that I've just started with python and I'm beginning to understand the basics.
I do not really ask for black on white code, rather a push in the right direction
I'd be glad to provide any further information if necessary! Thank you in advance!
Edit:
This is a bit late, but I would like to help anyone else who has had the same problem as I did. I eventually fixed my issue with the following:
def vert_condition(board, player):
"""The vert_condition iterates over the board and checks whether 4 vertical slots contain the same player input by
with the help of a nested loop"""
for row in range(3):
for col in range(6):
if (board[row][col] ==
board[row + 1][col] ==
board[row + 2][col] ==
board[row + 3][col] == player):
return True
python list if-statement
python list if-statement
edited Feb 10 at 12:45
CCG
asked Nov 16 '18 at 18:13
CCGCCG
44
44
Please explain what is your real problem.
– Mr Alihoseiny
Nov 16 '18 at 18:15
I can't come up with a beginning for the vertical condition
– CCG
Nov 16 '18 at 18:16
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
Nov 16 '18 at 18:16
@Prune I wish I could comply but I would not know what else to share. As I tried to explain, I wanted to get an idea on how to tackle a function which checks if the condition ( in this case 4 elements of the same player in one column one after each other ) has been met.
– CCG
Nov 16 '18 at 18:23
You've simply dropped some cognate code on us without any demonstration of your data structures, and none of the required instrumentation for us to test a suggestion or solution. I have little idea what acell
is, that you have to locate another instance withindex
. I don't know why2
is the boundary. I don't understand the repeated calls. As the posting guidelines tell you, please make it easy for us to help you.
– Prune
Nov 16 '18 at 18:23
|
show 1 more comment
Please explain what is your real problem.
– Mr Alihoseiny
Nov 16 '18 at 18:15
I can't come up with a beginning for the vertical condition
– CCG
Nov 16 '18 at 18:16
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
Nov 16 '18 at 18:16
@Prune I wish I could comply but I would not know what else to share. As I tried to explain, I wanted to get an idea on how to tackle a function which checks if the condition ( in this case 4 elements of the same player in one column one after each other ) has been met.
– CCG
Nov 16 '18 at 18:23
You've simply dropped some cognate code on us without any demonstration of your data structures, and none of the required instrumentation for us to test a suggestion or solution. I have little idea what acell
is, that you have to locate another instance withindex
. I don't know why2
is the boundary. I don't understand the repeated calls. As the posting guidelines tell you, please make it easy for us to help you.
– Prune
Nov 16 '18 at 18:23
Please explain what is your real problem.
– Mr Alihoseiny
Nov 16 '18 at 18:15
Please explain what is your real problem.
– Mr Alihoseiny
Nov 16 '18 at 18:15
I can't come up with a beginning for the vertical condition
– CCG
Nov 16 '18 at 18:16
I can't come up with a beginning for the vertical condition
– CCG
Nov 16 '18 at 18:16
2
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
Nov 16 '18 at 18:16
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
Nov 16 '18 at 18:16
@Prune I wish I could comply but I would not know what else to share. As I tried to explain, I wanted to get an idea on how to tackle a function which checks if the condition ( in this case 4 elements of the same player in one column one after each other ) has been met.
– CCG
Nov 16 '18 at 18:23
@Prune I wish I could comply but I would not know what else to share. As I tried to explain, I wanted to get an idea on how to tackle a function which checks if the condition ( in this case 4 elements of the same player in one column one after each other ) has been met.
– CCG
Nov 16 '18 at 18:23
You've simply dropped some cognate code on us without any demonstration of your data structures, and none of the required instrumentation for us to test a suggestion or solution. I have little idea what a
cell
is, that you have to locate another instance with index
. I don't know why 2
is the boundary. I don't understand the repeated calls. As the posting guidelines tell you, please make it easy for us to help you.– Prune
Nov 16 '18 at 18:23
You've simply dropped some cognate code on us without any demonstration of your data structures, and none of the required instrumentation for us to test a suggestion or solution. I have little idea what a
cell
is, that you have to locate another instance with index
. I don't know why 2
is the boundary. I don't understand the repeated calls. As the posting guidelines tell you, please make it easy for us to help you.– Prune
Nov 16 '18 at 18:23
|
show 1 more comment
1 Answer
1
active
oldest
votes
You could iterate in afor col in board
kind of way:
for col_index in range(6):
col = [row[col_index] for row in board]
for i in range(len(col) - 4 + 1):
if all(col[i + j] == player for j in range(4)):
return True
Since you can’t directly iterate over rows, iterate with an index counter - in this case a range(6)
- and each time pull out the n-th element of each row to construct the column.
I suggest a small amendment to your code also:
Where you keep writing row.index(cell)
, this is a heavy computation so calculate it once and store it in a variable so you can re-use the result instead of continuously re-calculating it when you could just as easily store it.
To make it even better, use the all
built-in which you provide an iterable and returns true if all elements are true.
In addition, you only need to check 6 - 4 + 1 (=3) times since....
- the first check covers 1st - 4th
- the second checks 2nd - 5th
- the third checks 3rd - 6th
And after that there’s no more as there isn’t a 7th cell in the row (and column). The calculation which I included in my code was:
len(row) - len(what’s being checked) + 1
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343309%2fconnect-4-trouble-with-the-vertical-winning-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could iterate in afor col in board
kind of way:
for col_index in range(6):
col = [row[col_index] for row in board]
for i in range(len(col) - 4 + 1):
if all(col[i + j] == player for j in range(4)):
return True
Since you can’t directly iterate over rows, iterate with an index counter - in this case a range(6)
- and each time pull out the n-th element of each row to construct the column.
I suggest a small amendment to your code also:
Where you keep writing row.index(cell)
, this is a heavy computation so calculate it once and store it in a variable so you can re-use the result instead of continuously re-calculating it when you could just as easily store it.
To make it even better, use the all
built-in which you provide an iterable and returns true if all elements are true.
In addition, you only need to check 6 - 4 + 1 (=3) times since....
- the first check covers 1st - 4th
- the second checks 2nd - 5th
- the third checks 3rd - 6th
And after that there’s no more as there isn’t a 7th cell in the row (and column). The calculation which I included in my code was:
len(row) - len(what’s being checked) + 1
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
add a comment |
You could iterate in afor col in board
kind of way:
for col_index in range(6):
col = [row[col_index] for row in board]
for i in range(len(col) - 4 + 1):
if all(col[i + j] == player for j in range(4)):
return True
Since you can’t directly iterate over rows, iterate with an index counter - in this case a range(6)
- and each time pull out the n-th element of each row to construct the column.
I suggest a small amendment to your code also:
Where you keep writing row.index(cell)
, this is a heavy computation so calculate it once and store it in a variable so you can re-use the result instead of continuously re-calculating it when you could just as easily store it.
To make it even better, use the all
built-in which you provide an iterable and returns true if all elements are true.
In addition, you only need to check 6 - 4 + 1 (=3) times since....
- the first check covers 1st - 4th
- the second checks 2nd - 5th
- the third checks 3rd - 6th
And after that there’s no more as there isn’t a 7th cell in the row (and column). The calculation which I included in my code was:
len(row) - len(what’s being checked) + 1
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
add a comment |
You could iterate in afor col in board
kind of way:
for col_index in range(6):
col = [row[col_index] for row in board]
for i in range(len(col) - 4 + 1):
if all(col[i + j] == player for j in range(4)):
return True
Since you can’t directly iterate over rows, iterate with an index counter - in this case a range(6)
- and each time pull out the n-th element of each row to construct the column.
I suggest a small amendment to your code also:
Where you keep writing row.index(cell)
, this is a heavy computation so calculate it once and store it in a variable so you can re-use the result instead of continuously re-calculating it when you could just as easily store it.
To make it even better, use the all
built-in which you provide an iterable and returns true if all elements are true.
In addition, you only need to check 6 - 4 + 1 (=3) times since....
- the first check covers 1st - 4th
- the second checks 2nd - 5th
- the third checks 3rd - 6th
And after that there’s no more as there isn’t a 7th cell in the row (and column). The calculation which I included in my code was:
len(row) - len(what’s being checked) + 1
You could iterate in afor col in board
kind of way:
for col_index in range(6):
col = [row[col_index] for row in board]
for i in range(len(col) - 4 + 1):
if all(col[i + j] == player for j in range(4)):
return True
Since you can’t directly iterate over rows, iterate with an index counter - in this case a range(6)
- and each time pull out the n-th element of each row to construct the column.
I suggest a small amendment to your code also:
Where you keep writing row.index(cell)
, this is a heavy computation so calculate it once and store it in a variable so you can re-use the result instead of continuously re-calculating it when you could just as easily store it.
To make it even better, use the all
built-in which you provide an iterable and returns true if all elements are true.
In addition, you only need to check 6 - 4 + 1 (=3) times since....
- the first check covers 1st - 4th
- the second checks 2nd - 5th
- the third checks 3rd - 6th
And after that there’s no more as there isn’t a 7th cell in the row (and column). The calculation which I included in my code was:
len(row) - len(what’s being checked) + 1
edited Nov 16 '18 at 18:31
answered Nov 16 '18 at 18:26
N ChauhanN Chauhan
1,8641215
1,8641215
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
add a comment |
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
I see! I will try to implement said amendment! Thank you!
– CCG
Nov 16 '18 at 18:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343309%2fconnect-4-trouble-with-the-vertical-winning-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please explain what is your real problem.
– Mr Alihoseiny
Nov 16 '18 at 18:15
I can't come up with a beginning for the vertical condition
– CCG
Nov 16 '18 at 18:16
2
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
Nov 16 '18 at 18:16
@Prune I wish I could comply but I would not know what else to share. As I tried to explain, I wanted to get an idea on how to tackle a function which checks if the condition ( in this case 4 elements of the same player in one column one after each other ) has been met.
– CCG
Nov 16 '18 at 18:23
You've simply dropped some cognate code on us without any demonstration of your data structures, and none of the required instrumentation for us to test a suggestion or solution. I have little idea what a
cell
is, that you have to locate another instance withindex
. I don't know why2
is the boundary. I don't understand the repeated calls. As the posting guidelines tell you, please make it easy for us to help you.– Prune
Nov 16 '18 at 18:23