SwiftUI: I want to use the information in my array but I'm unable to find the right command - arrays

I'm very new to programming and I'm having trouble with the more complex aspects. I am trying to use a data set called playerCameCards and randomly add 5 of these to playerCards. I have managed to do that but I want to display name: of the last added element but I can't figure out how to do it. I'd be grateful for some help Below is my code.
import Foundation
import SwiftUI
class Cards: ObservableObject {
#Published var playerCards = [gameCards]()
#Published var playerGameCards = [
gameCards(id: 1, name: "name1", attack: 92, defence: 49, gameControl: 60, creativity: 72, legend: 4),
gameCards(id: 2, name: "name2", attack: 87, defence: 40, gameControl: 65, creativity: 80, legend: 2),
gameCards(id: 3, name: "name3", attack: 43, defence: 93, gameControl: 40, creativity: 45, legend: 3),
gameCards(id: 4, name: "name4", attack: 88, defence: 51, gameControl: 80, creativity: 92, legend: 5),
gameCards(id: 5, name: "name5", attack: 85, defence: 51, gameControl: 72, creativity: 81, legend: 3),
gameCards(id: 6, name: "name6", attack: 91, defence: 38, gameControl: 72, creativity: 89, legend: 5),
gameCards(id: 7, name: "name7", attack: 34, defence: 95, gameControl: 40, creativity: 50, legend: 5),
gameCards(id: 8, name: "name8", attack: 86, defence: 63, gameControl: 89, creativity: 84, legend: 4),
gameCards(id: 9, name: "name9", attack: 90, defence: 30, gameControl: 50, creativity: 83, legend: 5),
gameCards(id: 10, name: "name10", attack: 32, defence: 92, gameControl: 42, creativity: 32, legend: 4)
]
}
struct gameCards: Identifiable {
let id: Int
let name: String
let attack: Int
let defence: Int
let gameControl: Int
let creativity: Int
let legend: Int
}
import SwiftUI
struct ContentView: View {
#EnvironmentObject var cardData: Cards
#State var firstTime: Bool = false
#State var currentCard: String = "Back"
var body: some View {
ZStack{
VStack{
Spacer()
Text("Lets open your 1st cards").background(Color.white).font(.title)
Spacer()
Below is where I want show the name e.g. "name1" from the last element added to my playerCards array. I may want to make this an image in the future and use the ID: to display the corresponding image but I'm unable to access the types in my array
Text("I want to show the "name" of the element in the last array I've just used").frame(width: 200, height: 200).background(Color.blue)
Spacer()
Button(action:{
if self.cardData.playerCards.count <= 4{
self.cardData.playerCards.append(self.cardData.playerGameCards.randomElement()!)
print("\([self.cardData.playerCards.last])")
}
if self.cardData.playerCards.count > 4 {
self.firstTime = true
}
}){
if self.cardData.playerCards.count <= 4 {
Image("Click").renderingMode(.original).resizable().frame(width: 100, height: 100)
}
else{
NavigationLink(destination: ContentView()){Image("Continue").renderingMode(.original).resizable().frame(width: 100, height: 100)
}
}
}
}
}
}
}

This is how to embed a value in a String in Swift:
if self.cardData.playerCards.last != nil {
Text("I want to show the \(self.cardData.playerCards.last!.name) of the element in the last array I've just used")
}

Related

Updating ReactJS UI with loop data one after another

I have recently created grid in UI and I want to show the complete flow of motion for all the points given by the array. However, the UI was updated with the value in the last loop.
For ex: position = [1, 2, 3]
At the initial point '1', there is a circle
then the circle move to 2 -> then 3
Instead, moving from 1 to 3
Can anybody help me to achieve this?
My whole class is as follows,
import React, { useEffect, useState } from 'react';
import { Button } from 'reactstrap';
import { motion } from "framer-motion";
import './style.css';
import Grid from './grid'
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
idOfBegin: "1",
position: 0,
xAxis: 0,
yAxis: 0,
position: [],
arrOfObj: [100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
]
};
}
start = () => {
var a = document.getElementById("3");
var codinates = a.getBoundingClientRect();
var x = codinates.left + 35;
var y = codinates.top + 5;
console.log(x, y);
this.setState({ xAxis: x });
this.setState({ yAxis: y });
}
loadPosition = async (event) => {
fetch('api/groups')
.then(response => response.json())
.then(data => {
//console.log('======== ' +data);
//groups.map(group => setPosition(group));
this.setState({ position: data });
});
}
getCellValue = async (event) => {
this.loadPosition();
this.state.position.forEach(p => {
console.log('====== ' + p);
if (p != null) {
var a = document.getElementById(p);
var codinates = a.getBoundingClientRect();
var x = codinates.left + 35;
var y = codinates.top + 5;
//console.log(x,y);
this.setState({ xAxis: x });
this.setState({ yAxis: y });
}
});
}
componentDidMount() {
this.setState({ xAxis: 45 });
this.setState({ yAxis: 905 });
}
render() {
return (
<div>
<div className="grid"><Grid arrOfObj={this.state.arrOfObj} />
<motion.div animate={{ x: this.state.xAxis, y: this.state.yAxis }}
transition={{ type: "tween", duration: 1 }}
></motion.div>
</div>
<div className="control">
<Button color="primary" onClick={() => this.start()}>Start</Button>
<Button color="primary" onClick={() => this.getCellValue()}>Roll Dice</Button>
</div>
</div>
);
}
}
export default Game;

Project Euler problem 11 in C isnt providing any output

I have been trying to solve Project Euler problem 11. I have rechecked my code multiple times but still, after debugging, my program is not providing any answers
The question states that we need to find the greatest product possible of 4 consecutive numbers: either vertically, horizontally or diagonally of the matrix given in the code below:
Here is my code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int matr[20][20] =
{
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};
int horProduct=0, verProduct=0, diagright=0, diagleft=0;
int maxProduct = 0;
// horizontal product
for (int i = 0; i < 20; i++)
{
for(int j = 0; j < 17; j++)
{
horProduct = matr[i][j]*matr[i][j+1]*matr[i][j+2]*matr[i][j+3];
}
if(horProduct > maxProduct)
{
maxProduct = horProduct;
}
}
//vertical product
for (int j = 0; j < 20; j++)
{
for(int i = 0; i < 17; i++)
{
verProduct = matr[i][j]*matr[i+1][j]*matr[i+2][j]*matr[i+3][j];
}
if(verProduct > maxProduct)
{
maxProduct = verProduct;
}
}
//diagonal right
for (int i = 0; i < 17; i++)
{
for(int j = 0; j < 17; j++)
{
diagright = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3];
}
if(diagright > maxProduct)
{
maxProduct = diagright;
}
}
//diagonal left
for (int i = 19; i > 3; i--)
{
for(int j = 19; j > 3; j--)
{
diagleft = matr[i][j]*matr[i-1][j-1]*matr[i-2][j-2]*matr[i-3][j-3];
}
if(diagleft > maxProduct)
{
maxProduct = diagleft;
}
}
printf("final largest product: %d\n", maxProduct);
return 0;
}
kindly let me know what is going wrong here. Why isn't my program able to print any output?
I did a rookie mistake.
The comparison with maxProduct should be inside the second loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int matr[20][20] =
{
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};
int horProduct=0, verProduct=0, diagright=0, diagleft=0;
int maxProduct = 0;
// horizontal product
for (int i = 0; i < 20; i++)
{
for(int j = 0; j < 17; j++)
{
horProduct = matr[i][j]*matr[i][j+1]*matr[i][j+2]*matr[i][j+3];
if(horProduct > maxProduct)
{
maxProduct = horProduct;
}
}
}
printf("hori: %d\n", maxProduct);
for (int j = 0; j < 20; j++)
{
for(int i = 0; i < 17; i++)
{
verProduct = matr[i][j]*matr[i+1][j]*matr[i+2][j]*matr[i+3][j];
if(verProduct > maxProduct)
{
maxProduct = verProduct;
}
}
}
printf("verti: %d\n", maxProduct);
//diagonal right
for (int i = 0; i < 17; i++)
{
for(int j = 0; j < 17; j++)
{
diagright = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3];
if(diagright > maxProduct)
{
maxProduct = diagright;
}
}
}
printf("diagright: %d\n", maxProduct);
//diagonal left
for (int i = 0; i < 20 ; i++)
{
for(int j = 0; j < 17; j++)
{
diagleft = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3];
if(diagleft > maxProduct)
{
maxProduct = diagleft;
}
}
}
printf("diagleft: %d\n", maxProduct);
return 0;
}

SwiftUI: I have a list which when I click on an object I want to go into a detail view where I can select or deselect the current object

I can set up the list fine and even have a select or deselect button in the detail view (CardView). The problem I encounter is when I click select I want to add it to an array (cardSelected). If "name3" is the 1st in the list how can I find out the index to add to the cardSelected array?
I'm new to programming so looking for some guidance. Thanks
import Foundation
import SwiftUI
class Cards: ObservableObject {
#Published var cardSelected = [gameCards]()
#Published var playerCards = [gameCards]()
#Published var playerGameCards = [
gameCards(id: 1, name: "name1", attack: 92, defence: 49, gameControl: 60, creativity: 72, legend: 4),
gameCards(id: 2, name: "name2", attack: 87, defence: 40, gameControl: 65, creativity: 80, legend: 2),
gameCards(id: 3, name: "name3", attack: 43, defence: 93, gameControl: 40, creativity: 45, legend: 3),
gameCards(id: 4, name: "name4", attack: 88, defence: 51, gameControl: 80, creativity: 92, legend: 5),
gameCards(id: 5, name: "name5", attack: 85, defence: 51, gameControl: 72, creativity: 81, legend: 3),
gameCards(id: 6, name: "name6", attack: 91, defence: 38, gameControl: 72, creativity: 89, legend: 5),
gameCards(id: 7, name: "name7", attack: 34, defence: 95, gameControl: 40, creativity: 50, legend: 5),
gameCards(id: 8, name: "name8", attack: 86, defence: 63, gameControl: 89, creativity: 84, legend: 4),
gameCards(id: 9, name: "name9", attack: 90, defence: 30, gameControl: 50, creativity: 83, legend: 5),
gameCards(id: 10, name: "name10", attack: 32, defence: 92, gameControl: 42, creativity: 32, legend: 4)
]
}
struct gameCards: Identifiable {
let id: Int
let name: String
let attack: Int
let defence: Int
let gameControl: Int
let creativity: Int
let legend: Int
import SwiftUI
struct CardView: View{
var carddetail: String
#EnvironmentObject var cardselected: Cards
#State var cardSelect: Bool = false
var body: some View{
ZStack{
VStack{
Text(carddetail)
Spacer()
if cardSelect == false {
Button(action:{
self.cardSelect = true
below ?? is where I want to add the index but don't know how to programmatically find it.
self.cardselected.cardSelected.append(self.cardselected.playerCards[??])
print("\([self.carddetail])")
print("\([self.cardselected.cardSelected.description])")
}){
if cardSelect == false {
Image(systemName: "star")
}
if cardSelect == true{
Image(systemName: "star.fill")
}
}
}
if cardSelect == true {
Button(action:{
self.cardSelect = false
}){
if cardSelect == false {
Image(systemName: "star")
}
if cardSelect == true{
Image(systemName: "star.fill")
}
}
}
Spacer()
}
}
}
}
struct ContentView: View {
#EnvironmentObject var playerList: Cards
var body: some View {
NavigationView{
ZStack{
Image("Background").resizable().edgesIgnoringSafeArea(.all)
VStack{
Image("FLTitle").resizable().frame(width: 400, height: 100)
Spacer()
Text("Collected Cards")
Text("Choose 5")
ScrollView(.horizontal){
HStack{
Image("Back")
Image("Back")
Image("Back")
Image("Back")
Image("Back")
}.padding(.horizontal)
}
Spacer()
HStack{
NavigationView{
List(playerList.playerCards) {gc in
NavigationLink(destination: CardView(carddetail: gc.name)){
Text("\(gc.name)")
}
}.navigationBarTitle("Card detail").font(.body)
}.padding(.leading).frame(width: UIScreen.main.bounds.size.width/2, height: 200)
Text("Opposition").padding(.trailing).frame(width: UIScreen.main.bounds.size.width/2, height: 200).background(Color.red)
}
Spacer()
}
}
}.navigationBarTitle("", displayMode: .inline).navigationBarBackButtonHidden(true).navigationBarHidden(true)
}
}
let data = Cards()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(data)
}
}
When creating the List you can use ForEach to access the index and then pass to CardView
List {
ForEach(0..<playerList.playerCards.count) { index in
NavigationLink(destination: CardView(carddetail: self.playerList.playerCards[index].name,
index: index)) {
Text("\(self.playerList.playerCards[index].name)")
}
}.navigationBarTitle("Card detail").font(.body)
}

Split string to a 2-dimensional array in Scala

In Scala:
Is there a way to directly split a string that contains 72 numeric values separated by ; into a 2-dimensional array of 9 rows and 8 columns with those numeric values -in numeric data type-?
val input = List.tabulate(72)(_.toString).mkString(";")
input.split(";").map(_.toInt).grouped(9).toArray
transforms
0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
into
Array(
Array(0, 1, 2, 3, 4, 5, 6, 7, 8),
Array(9, 10, 11, 12, 13, 14, 15, 16, 17),
Array(18, 19, 20, 21, 22, 23, 24, 25, 26),
Array(27, 28, 29, 30, 31, 32, 33, 34, 35),
Array(36, 37, 38, 39, 40, 41, 42, 43, 44),
Array(45, 46, 47, 48, 49, 50, 51, 52, 53),
Array(54, 55, 56, 57, 58, 59, 60, 61, 62),
Array(63, 64, 65, 66, 67, 68, 69, 70, 71)
)
If you want to swap the dimensions of rows/columns, replace 9 by 8.
using Range and grouped functions
scala> val a = (0 to 71).map(_.toString).toArray.mkString(";")
a: String = 0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
scala> a.split(";").map(_.toInt).sliding(9,9).toArray
res269: Array[Array[Int]] = Array(Array(0, 1, 2, 3, 4, 5, 6, 7, 8), Array(9, 10, 11, 12, 13, 14, 15, 16, 17), Array(18, 19, 20, 21, 22, 23, 24, 25, 26), Array(27, 28, 29, 30, 31, 32, 33, 34, 35), Array(36, 37, 38, 39, 40, 41, 42, 43, 44), Array(45, 46, 47, 48, 49, 50, 51, 52, 53), Array(54, 55, 56, 57, 58, 59, 60, 61, 62), Array(63, 64, 65, 66, 67, 68, 69, 70, 71))
scala>

Creating a Dictionary: Assigning an Array of Ints as Keys to another array

I have an array of Arrays and an Array of Integers and want to create a dictionary with the Integers Array elements as keys to elements in the Array of Array.
I have tried a number of iteration methods without much luck. Any thoughts or ideas?
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var Dictionary : [Int: [Int]] = [:]
Try this:
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var Dictionary = [Int: [Int]]()
// Swift 2.0
for (index, key) in IntegerKeys.enumerate() {
Dictionary[key] = populationArray[index]
}
// Swift 1.2
for (index, key) in enumerate(IntegerKeys) {
Dictionary[key] = populationArray[index]
}
#ZoffDino's answer does work, however it would crash if the populationArray would contain less elements than integerKeys. I'm proposing a method that doesn't have this flaw:
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var dictionary : [Int: [Int]] = [:]
for (key, value) in zip(IntegerKeys, populationArray) {
dictionary[key] = value
}

Resources