gpt4 book ai didi

java - 如何用for循环简化这段代码?

转载 作者:行者123 更新时间:2023-12-01 21:53:06 32 4
gpt4 key购买 nike



对于我的学校项目,我们正在创建一个纸牌游戏,我有一些硬编码的巨大代码块。我希望有人能够看到我附加的代码中的模式,并能够使用 for 循环来压缩它。

    // set the pointers of the cards in the pyramid
card [0].setPtrs (card [1], card [2]);
card [1].setPtrs (card [3], card [4]);
card [2].setPtrs (card [4], card [5]);
card [3].setPtrs (card [6], card [7]);
card [4].setPtrs (card [7], card [8]);
card [5].setPtrs (card [8], card [9]);
card [6].setPtrs (card [10], card [11]);
card [7].setPtrs (card [11], card [12]);
card [8].setPtrs (card [12], card [13]);
card [9].setPtrs (card [13], card [14]);
card [10].setPtrs (card [15], card [16]);
card [11].setPtrs (card [16], card [17]);
card [12].setPtrs (card [17], card [18]);
card [13].setPtrs (card [18], card [19]);
card [14].setPtrs (card [19], card [20]);
card [15].setPtrs (card [21], card [22]);
card [16].setPtrs (card [22], card [23]);
card [17].setPtrs (card [23], card [24]);
card [18].setPtrs (card [24], card [25]);
card [19].setPtrs (card [25], card [26]);
card [20].setPtrs (card [26], card [27]);

// set card X coords
cardCentreX [0] = 800;
cardCentreX [1] = 800 - card [1].getWidth () / 2;
cardCentreX [2] = 800 + card [1].getWidth () / 2;
cardCentreX [3] = cardCentreX [1] - card [1].getWidth () / 2;
cardCentreX [4] = cardCentreX [2] - card [1].getWidth () / 2;
cardCentreX [5] = cardCentreX [2] + card [1].getWidth () / 2;
cardCentreX [6] = cardCentreX [3] - card [1].getWidth () / 2;
cardCentreX [7] = cardCentreX [4] - card [1].getWidth () / 2;
cardCentreX [8] = cardCentreX [5] - card [1].getWidth () / 2;
cardCentreX [9] = cardCentreX [5] + card [1].getWidth () / 2;
cardCentreX [10] = cardCentreX [6] - card [1].getWidth () / 2;
cardCentreX [11] = cardCentreX [7] - card [1].getWidth () / 2;
cardCentreX [12] = cardCentreX [8] - card [1].getWidth () / 2;
cardCentreX [13] = cardCentreX [9] - card [1].getWidth () / 2;
cardCentreX [14] = cardCentreX [9] + card [1].getWidth () / 2;
cardCentreX [15] = cardCentreX [10] - card [1].getWidth () / 2;
cardCentreX [16] = cardCentreX [11] - card [1].getWidth () / 2;
cardCentreX [17] = cardCentreX [12] - card [1].getWidth () / 2;
cardCentreX [18] = cardCentreX [13] - card [1].getWidth () / 2;
cardCentreX [19] = cardCentreX [14] - card [1].getWidth () / 2;
cardCentreX [20] = cardCentreX [14] + card [1].getWidth () / 2;
cardCentreX [21] = cardCentreX [15] - card [1].getWidth () / 2;
cardCentreX [22] = cardCentreX [16] - card [1].getWidth () / 2;
cardCentreX [23] = cardCentreX [17] - card [1].getWidth () / 2;
cardCentreX [24] = cardCentreX [18] - card [1].getWidth () / 2;
cardCentreX [25] = cardCentreX [19] - card [1].getWidth () / 2;
cardCentreX [26] = cardCentreX [20] - card [1].getWidth () / 2;
cardCentreX [27] = cardCentreX [20] + card [1].getWidth () / 2;

for (int i = 0 ; i < 28 ; i++)
{
cardCentreY [i] = 50;
if (i == 1 || i == 2)
{
cardCentreY [i] = cardCentreY [0] + card [1].getHeight () / 2;
}
else if (i >= 3 && i <= 5)
{
cardCentreY [i] = cardCentreY [1] + card [1].getHeight () / 2;
}
else if (i >= 6 && i <= 9)
{
cardCentreY [i] = cardCentreY [3] + card [1].getHeight () / 2;
}
else if (i >= 10 && i <= 14)
{
cardCentreY [i] = cardCentreY [6] + card [1].getHeight () / 2;
}
else if (i >= 15 && i <= 20)
{
cardCentreY [i] = cardCentreY [10] + card [1].getHeight () / 2;
}
else if (i >= 21 && i <= 27)
{
cardCentreY [i] = cardCentreY [15] + card [1].getHeight () / 2;
}
}

最佳答案

您没有提供最小的工作示例,这使得测试任何可能的解决方案以确保它们与您的代码执行相同的操作变得困难。我用 JavaScript 而不是 Java 制作了一个简单版本的原型(prototype),这让我可以进行一些简单的可视化,以检查我的循环是否正常工作。我假设您能够毫不费力地将 JavaScript 逻辑转换回 Java。

这是循环(在 JavaScript 中):

var x_row_base = 250; // You used 800 here
var y = 0;
var cards = Array(28);

// Set up card pyramid
for (var i = 0, row = 0; i < cards.length; row++) {
var x = x_row_base;
for (var col = 0; col < row; i++, col++) {
cards[i] = make_card(i, row);
// set x and y position
cards[i].x = x;
cards[i].y = y;
// create links to "child" cards in next row
if (i < 21) {
cards[i].left_child_index = i + row;
cards[i].right_child_index = i + row + 1;
}
// display the card
draw_card(cards[i]);
x += CARD_X_SPACING + CARD_X_PADDING;
}
y += CARD_Y_SPACING;
x_row_base -= (CARD_X_SPACING + CARD_X_PADDING) / 2;
}

您可以使用下面的代码片段 ( or on jsfiddle ) 进行尝试。当您将鼠标悬停在父卡上时,通过更改子卡的颜色来显示父/子卡链接。以下是 JavaScript 可视化在我的笔记本电脑上的屏幕截图:

enter image description here

function draw_card(card) {
var $card = $("<div/>");
$card.text(card.label);
$card.addClass('card');
$card.css({
'left': card.x,
'top': card.y,
'z-index': card.z
});
$card.appendTo($('#my_canvas'));
card.rect = $card;
// last row doesn't actually have children
if (card.left_child_index) {
$card.mouseover(function() {
cards[card.left_child_index].rect.addClass("child");
cards[card.right_child_index].rect.addClass("child");
});
$card.mouseout(function() {
cards[card.left_child_index].rect.removeClass("child");
cards[card.right_child_index].rect.removeClass("child");
});
}
}

function make_card(label, z) {
return {
label: label,
z: z
};
}

var CARD_X_PADDING = 4;
var CARD_X_SPACING = 50;
var CARD_Y_SPACING = 45;

var x_row_base = 250; // You used 800 here
var y = 0;
var cards = Array(28);

// Set up card pyramid
for (var i = 0, row = 0; i < cards.length; row++) {
var x = x_row_base;
for (var col = 0; col < row; i++, col++) {
cards[i] = make_card(i, row);
// set x and y position
cards[i].x = x;
cards[i].y = y;
// create links to "child" cards in next row
if (i < 21) {
cards[i].left_child_index = i + row;
cards[i].right_child_index = i + row + 1;
}
// display the card
draw_card(cards[i]);
x += CARD_X_SPACING + CARD_X_PADDING;
}
y += CARD_Y_SPACING;
x_row_base -= (CARD_X_SPACING + CARD_X_PADDING) / 2;
}
.card {
border: 1px solid black;
text-align: center;
position: absolute;
width: 50px;
padding: 20px 0px;
background-color: gray;
font-size: 15px;
cursor: pointer;
}
.child {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my_canvas"></div>

关于java - 如何用for循环简化这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845740/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com